{"id":2322,"date":"2021-01-15T11:00:53","date_gmt":"2021-01-15T11:00:53","guid":{"rendered":"https:\/\/thenextweb.com\/?p=1334144"},"modified":"2021-01-15T11:00:53","modified_gmt":"2021-01-15T11:00:53","slug":"how-to-manipulate-date-and-time-in-javascript","status":"publish","type":"post","link":"https:\/\/www.londonchiropracter.com\/?p=2322","title":{"rendered":"How to manipulate date and time in JavaScript"},"content":{"rendered":"\n<p>In theory, handling dates as a developer is as simple as creating, storing, and, if necessary, manipulating dates. But as a JavaScript developer, you would know this theory doesn\u2019t hold long after you start working with dates for real. On top of different date-time formats, you have to consider timezone and local differences.<\/p>\n<p>For this reason, plenty of JavaScript developers seek help from third-party libraries when they have to manage dates in an application. While these libraries reduce the task\u2019s complexity, having a clear understanding of handling vanilla JavaScript dates has its benefits.<\/p>\n<p>This tutorial will introduce you to working with dates in vanilla JavaScript, as well as useful third-party libraries to help you simplify more complex date-related tasks.<\/p>\n<h2 id=\"javascript-date-object\">JavaScript Date object<\/h2>\n<p>The Date object in JavaScript is the main element when it comes to handling date and time. It records a single point in time as the milliseconds\u2019 number elapsed since the 1st January 1970 00:00:00 (UTC). This date-time combination is known as the epoch time. As far as JavaScript is concerned, it\u2019s the beginning of time in the world.<\/p>\n<h2 id=\"creating-dates\">Creating Dates<\/h2>\n<p>You can simply create a date using<span>&nbsp;<\/span><code>new Date()<\/code><span>&nbsp;<\/span>. You can pass parameters to the Date constructor to create a date of your choice. The given parameter can take different forms.<\/p>\n<p id=\"pass-a-date-string\"><strong>Pass a date string<\/strong><\/p>\n<p>You can pass a date string of an accepted format when creating a new Date object.<\/p>\n<p><em>const date = new Date (\u201c2020-12-31\u201d);<\/em><\/p>\n<p>Now, if we print the created date, it shows this.<\/p>\n<p class=\"p1\"><em>Thu Dec 31 2020 01:00:00 GMT+0100 (Central European Standard Time)<\/em><\/p>\n<p>In addition to the date we passed, the date object has more values, including a time and a timezone. Since we didn\u2019t give a specific value for these parameters when creating the object, JavaScript uses the local time and timezone of the code\u2019s system.<\/p>\n<p>If we want to pass the time or timezone with the parameter string, we can use a format like this.<\/p>\n<p><strong>YYYY-MM-DDTHH:mm:ss.sssZ<\/strong><\/p>\n<ul>\n<li>YYYY: year<\/li>\n<li>MM: month (1 to 12)<\/li>\n<li>DD: date (1 to 31)<\/li>\n<li>HH: hour in 24-hour format (0 to 23)<\/li>\n<li>mm: minutes (0 to 59)<\/li>\n<li>ss: seconds (00 to 59)<\/li>\n<li>sss: milliseconds (0 to 999)<\/li>\n<li>T is used to separate the date and time in the string<\/li>\n<li>If Z is present, the time is assumed to be in UTC. Otherwise, it assumes the local time.<\/li>\n<\/ul>\n<p>However, if T and Z are not present, the string\u2019s created date may give different results in different browsers. In that case, to always have the same timezone for the date, add<span>&nbsp;<\/span><code>+HH:mm<\/code><span>&nbsp;<\/span>or<span>&nbsp;<\/span><code>-HH:mm<\/code><span>&nbsp;<\/span>to the end.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1334146 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.12.png\" alt width=\"846\" height=\"372\" sizes=\"(max-width: 846px) 100vw, 846px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.12.png 846w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.12-280x123.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.12-540x237.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.12-270x119.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.12-796x350.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>You can get the same results using the<span>&nbsp;<\/span><code>Date.parse<\/code><span>&nbsp;<\/span>function instead of passing the date string to the Date constructor.<span>&nbsp;<\/span><code>Date.parse<\/code><span>&nbsp;<\/span>is indirectly being called inside the constructor whenever you pass a date string.<\/p>\n<p>The format used in these strings is the ISO 8601 calendar extended format. You can refer to its details in the<span>&nbsp;<\/span><a href=\"https:\/\/tc39.es\/ecma262\/#sec-date-time-string-format\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">ECMAScript specification<\/a><span>&nbsp;<\/span>.<\/p>\n<p id=\"pass-date-arguments\">Pass date arguments<\/p>\n<p>You can directly pass the date arguments to the Date constructor without using confusing date strings. The order and length of each year, month, etc., are exactly as in a date string.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1334147 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.18.png\" alt width=\"876\" height=\"44\" sizes=\"(max-width: 876px) 100vw, 876px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.18.png 876w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.18-280x14.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.18-540x27.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.18-270x14.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.18-796x40.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>When we inspect the created date\u2019s outcome, we can notice one crucial difference in the final date.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1334148 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.28.png\" alt width=\"879\" height=\"47\" sizes=\"(max-width: 879px) 100vw, 879px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.28.png 898w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.28-280x15.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.28-540x29.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.28-270x14.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.56.28-796x43.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>What\u2019s weird? When we created the date, we used 9 for the month, which we could assume to be September. However, when we print the result, the month is October instead. Why is that?<\/p>\n<p>JavaScript uses a zero-based index to identify each month in a year. This means, for JavaScript, January is represented by 0 instead of 1. Similarly, October is represented by 9 instead of 10.<\/p>\n<p>In this method of creating a date, we can\u2019t pass an argument to indicate its time zone. So, it\u2019s defaulted to the local time of the system. But we can use the<span>&nbsp;<\/span><code>Date.UTC<\/code><span>&nbsp;<\/span>function to convert the date to UTC before passing it to the Date constructor.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1334149 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.42.png\" alt width=\"860\" height=\"106\" sizes=\"(max-width: 860px) 100vw, 860px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.42.png 860w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.42-280x35.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.42-540x67.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.42-270x33.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.42-796x98.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p id=\"pass-a-timestamp\">Pass a timestamp<\/p>\n<p>Remember that I mentioned JavaScript stores the time elapsed since the epoch time in the Date object? We can pass this elapsed time value, called a timestamp, to indicate the date we are creating.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1334150 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.47.png\" alt width=\"848\" height=\"114\" sizes=\"(max-width: 848px) 100vw, 848px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.47.png 848w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.47-280x38.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.47-540x73.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.47-270x36.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.47-796x107.png 796w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.47-840x114.png 840w\"><\/figure>\n<p><\/span><\/p>\n<p id=\"create-a-date-object-for-the-current-date-and-time\">Create a Date object for the current date and time<\/p>\n<p>If you want to create a Date object for the current date and time of the system, use the Date constructor without passing any argument.<\/p>\n<p><figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1334151 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.51.png\" alt width=\"868\" height=\"108\" sizes=\"(max-width: 868px) 100vw, 868px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.51.png 868w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.51-280x35.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.51-540x67.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.51-270x34.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.51-796x99.png 796w\"><\/figure>\n<\/p>\n<p>You can also use the<span>&nbsp;<\/span><code>Date.now()<\/code><span>&nbsp;<\/span>function for the same task.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1334152 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.54.png\" alt width=\"286\" height=\"54\" sizes=\"(max-width: 286px) 100vw, 286px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.54.png 286w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.54-280x53.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.57.54-270x51.png 270w\"><\/figure>\n<p><\/span><\/p>\n<h2 id=\"formatting-dates\">Formatting dates<\/h2>\n<p>JavaScript provides several built-in functions to format a date. However, these functions only convert the date to a format specific to each one.<\/p>\n<p>Let\u2019s see how each formatting function works.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1334153 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.59.30.png\" alt width=\"787\" height=\"1217\" sizes=\"(max-width: 787px) 100vw, 787px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.59.30.png 882w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.59.30-136x210.png 136w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.59.30-175x270.png 175w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.59.30-87x135.png 87w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.59.30-796x1231.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p id=\"internationalization-api\"><strong>Internationalization API<\/strong><\/p>\n<p>ECMAScript<span>&nbsp;<\/span><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Intl\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Internationalization API<\/a><span>&nbsp;<\/span>allows the formatting of a date into a specific locale using the Intl object.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1334154 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.59.51.png\" alt width=\"785\" height=\"373\" sizes=\"(max-width: 785px) 100vw, 785px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.59.51.png 854w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.59.51-280x133.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.59.51-540x257.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.59.51-270x128.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-15.59.51-796x378.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>You can pass an options object to the DateTimeFormat function to display time values and customize the output.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1334155 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.00.30.png\" alt width=\"806\" height=\"435\" sizes=\"(max-width: 806px) 100vw, 806px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.00.30.png 860w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.00.30-280x151.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.00.30-500x270.png 500w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.00.30-250x135.png 250w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.00.30-796x429.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p id=\"custom-date-formats\"><strong>Custom date formats<\/strong><\/p>\n<p>If you want to format the date to any other format beyond what these functions provide, you\u2019ll have to do so by accessing each part of the date separately and combining them.<\/p>\n<p>JavaScript provides the following functions to retrieve the year, month, date, and day from a Date object.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1334156 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.01.29.png\" alt width=\"890\" height=\"364\" sizes=\"(max-width: 890px) 100vw, 890px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.01.29.png 890w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.01.29-280x115.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.01.29-540x221.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.01.29-270x110.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.01.29-796x326.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>Now, you can convert the date to a custom format using retrieved parts.<\/p>\n<h2 id=\"updating-dates\">Updating dates<\/h2>\n<p>JavaScript provides several methods to edit an already created date.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1334157 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.01.37.png\" alt width=\"826\" height=\"676\" sizes=\"(max-width: 826px) 100vw, 826px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.01.37.png 826w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.01.37-257x210.png 257w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.01.37-330x270.png 330w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.01.37-165x135.png 165w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.01.37-796x651.png 796w\"><\/figure>\n<p><\/span><\/p>\n<h2 id=\"comparing-dates\">Comparing dates<\/h2>\n<p>If you want to know whether a specific date comes before another, you can use greater than and less than operators directly for comparison.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1334158 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.02.19.png\" alt width=\"828\" height=\"821\" sizes=\"(max-width: 828px) 100vw, 828px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.02.19.png 936w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.02.19-96x96.png 96w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.02.19-212x210.png 212w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.02.19-272x270.png 272w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.02.19-136x135.png 136w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.02.19-796x789.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>This is because Dates in JavaScript are objects, so each date has a different instance of the class, and the<span>&nbsp;<\/span><code>==<\/code><span>&nbsp;<\/span>or<span>&nbsp;<\/span><code>===<\/code><span>&nbsp;<\/span>operator are comparing the memory address instead of the actual values of the dates.<\/p>\n<h2 id=\"javascript-date-manipulation-libraries\">JavaScript date manipulation libraries<\/h2>\n<p>We can find several JavaScript date and time manipulation libraries as open-source projects or otherwise. Some of them, designed for all kinds of date-time manipulations, and some have a specific set of use cases. In this section, I\u2019ll only talk about popular multi-purpose libraries.<\/p>\n<p>Moment.js used to be the king of date manipulation libraries among JavaScript developers. However,<span>&nbsp;<\/span><a href=\"https:\/\/momentjs.com\/docs\/#\/-project-status\/recommendations\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">its developers recently announced<\/a><span>&nbsp;<\/span>that it\u2019s focusing on maintaining the current codebase instead of adding new features. They recommend looking for an alternative solution for those who are working on new projects.<\/p>\n<p>So, apart from Moment.js, what are the libraries we can use to make our life easier as developers?<\/p>\n<h3 id=\"date-fns\">Date-fns<\/h3>\n<p><a href=\"https:\/\/date-fns.org\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Date-fns<\/a><span>&nbsp;<\/span>in an open-source library supporting date parsing and formatting, locales, and date arithmetic like addition and subtraction. It\u2019s dubbed as Lodash for dates due to its versatility.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1334159 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.03.08.png\" alt width=\"858\" height=\"882\" sizes=\"(max-width: 858px) 100vw, 858px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.03.08.png 920w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.03.08-204x210.png 204w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.03.08-263x270.png 263w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.03.08-131x135.png 131w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.03.08-796x818.png 796w\"><\/figure>\n<p><\/span><\/p>\n<h3 id=\"luxon\">Luxon<\/h3>\n<p><a href=\"https:\/\/github.com\/moment\/luxon\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Luxon<\/a><span>&nbsp;<\/span>is a date-time manipulation library created by one of the Moment.js developers to suit modern application requirements. Similar to Date-fns, Luxon offers data formatting and parsing functions. Also, it has native Intl support and is chainable.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1334161 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.03.13.png\" alt width=\"815\" height=\"533\" sizes=\"(max-width: 815px) 100vw, 815px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.03.13.png 924w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.03.13-280x183.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.03.13-413x270.png 413w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.03.13-207x135.png 207w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-14-at-16.03.13-796x520.png 796w\"><\/figure>\n<p><\/span><\/p>\n<h2 id=\"summary\">Summary<\/h2>\n<p>This tutorial discussed how to work with date and time in JavaScript with and without external libraries. Working with dates is always painful in almost (if not all) programming languages. Fortunately for us, JS and its ecosystem of libraries does all the heavy work for us, allowing us to focus on building features.<\/p>\n<p><i><span>This <\/span><\/i><a href=\"https:\/\/livecodestream.dev\/post\/date-manipulation-in-javascript-a-complete-guide\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>article<\/span><\/i><\/a><i><span> was originally published on <\/span><\/i><a href=\"https:\/\/livecodestream.dev\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Live Code Stream<\/span><\/i><\/a><i><span> by <\/span><\/i><a href=\"https:\/\/www.linkedin.com\/in\/bajcmartinez\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Juan Cruz Martinez<\/span><\/i><\/a><i><span> (twitter: <\/span><\/i><a href=\"https:\/\/twitter.com\/bajcmartinez\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>@bajcmartinez<\/span><\/i><\/a><i><span>), founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker, and doer of things.<\/span><\/i><\/p>\n<p><a href=\"https:\/\/livecodestream.dev\/subscribe\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Live Code Stream<\/span><\/i><\/a><i><span> is also available as a free weekly newsletter. Sign up for updates on everything related to programming, AI, and computer science in general.<\/span><\/i><\/p>\n<p> <a href=\"https:\/\/thenextweb.com\/syndication\/2021\/01\/15\/how-to-manipulate-date-and-time-in-javascript\/\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In theory, handling dates as a developer is as simple as creating, storing, and, if necessary, manipulating dates. But as a JavaScript developer, you would know this theory doesn\u2019t hold long after&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2323,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/2322"}],"collection":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2322"}],"version-history":[{"count":0,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/2322\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/media\/2323"}],"wp:attachment":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}