{"id":67844,"date":"2024-12-02T10:23:49","date_gmt":"2024-12-02T04:53:49","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=67844"},"modified":"2025-09-29T17:32:52","modified_gmt":"2025-09-29T12:02:52","slug":"working-with-dates-in-javascript","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/working-with-dates-in-javascript\/","title":{"rendered":"Working with Dates in JavaScript"},"content":{"rendered":"\n<p>Working with dates and times is a fundamental part of many JavaScript applications, from scheduling events and logging activities to displaying timestamps and performing date calculations. JavaScript provides the <code>Date<\/code> object as a built-in tool for creating, manipulating, and formatting dates and times.<\/p>\n\n\n\n<p>In this article, we\u2019ll take a comprehensive look at working with dates in JavaScript. We\u2019ll start by exploring how to create and initialize dates using the <code>Date<\/code> object, followed by common operations like formatting, parsing, and manipulating dates.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Date object in JavaScript<\/h2>\n\n\n\n<p>One built-in datatype that facilitates working with dates and times is the <a href=\"https:\/\/www.guvi.in\/hub\/javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript <\/a>Date object. It offers several techniques for creating, modifying, and formatting dates.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating Date objects: &#8211;<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Current Date and Time<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>const now = new Date();<br>console.log(now, typeof now) \/\/ 2024-10-03T02:43:23.173Z object<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Timestamp<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>const timestamp = Date.now(); \/\/ 1727923507074<br><br>const dateFromTimestamp = new Date(timestamp);<br>console.log(dateFromTimestamp)\/\/ 2024-10-03T02:45:07.074Z<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Creating Date object using string<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>const dateString = Date();<br>console.log(dateString, typeof dateString)<br>\/\/ Thu Oct 03 2024 08:18:56 GMT+0530 (India Standard Time) string<br><br>const dateFromString = new Date(dateString);<br>console.log(dateFromString, typeof dateFromString)<br>\/\/ 2024-10-03T02:49:58.000Z object<br>const dateStringTwo = &#8220;15 August 2024&#8221;;<br><br>const dateFromStringTwo = new Date(dateStringTwo);<br>console.log(dateFromStringTwo, typeof dateFromStringTwo);<br>\/\/ 2024-08-15T00:00:00.000Z object<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Retrieving Date components: &#8211;<\/h3>\n\n\n\n<p>Once you have a Date object, you can retrieve multiple components using built-in methods&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>const dateObject = new Date(); \/\/ 2024-10-03T03:09:24.714Z<br><br><br>console.log(dateObject.getFullYear()); \/\/ 2024 number<br>console.log(dateObject.getMonth()); \/\/ 9 number<br>console.log(dateObject.getDate());&nbsp; \/\/ 3 number<br>console.log(dateObject.getDay()); \/\/ 4 for Thursday (0 = Sunday)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Date using the Moment.js library<\/h2>\n\n\n\n<p>A popular JavaScript library called Moment.js makes manipulating dates and times easier. Compared to the native JavaScript Date object, it offers a richer feature set that makes working with dates more straightforward.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Features of Moment.js<\/h3>\n\n\n\n<ul>\n<li><strong>Simplified Date Manipulation: <\/strong>Moment.js makes it simple to manipulate dates by providing methods for adding and subtracting time units (days, months, and years).<\/li>\n\n\n\n<li><strong>Flexible Parsing:<\/strong> The library can parse dates from numerous formats, making managing user input or data from different sources easier.<\/li>\n\n\n\n<li><strong>Formatting:<\/strong> Moment.js provides powerful formatting options to display dates in any desired format.<\/li>\n\n\n\n<li><strong>Relative Time:<\/strong> With built-in methods, you may quickly represent relative time (e.g.: &#8211; 7 days ago).<\/li>\n\n\n\n<li><strong>Validation:<\/strong> includes methods for validating date formats and checking if a date is valid.<\/li>\n<\/ul>\n\n\n\n<p>Install the moment.js node package using &#8211; <strong>npm install moment&nbsp;<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Creating moment instances<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>const date = moment();<br>console.log(date, typeof date) \/\/ Moment&lt;2024-10-03T09:03:48+05:30&gt; object<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>const dateString = &#8220;2024-08-15&#8221;;<br><br>const dateFromString = moment(dateString);<br>console.log(dateFromString, typeof dateFromString);<br>\/\/ Moment&lt;2024-08-15T00:00:00+05:30&gt; object<br><br>const parsedDate = moment(&#8220;03\/10\/2024&#8221;, &#8220;DD\/MM\/YYYY&#8221;);<br>console.log(parsedDate, typeof parsedDate);<br>\/\/ Moment&lt;2024-10-03T00:00:00+05:30&gt; object<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Formatting dates<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>const formattedDateOne = moment().format(&#8220;DD-MM-YYYY&#8221;);<br>console.log(formattedDateOne, typeof formattedDateOne);<br>\/\/ 03-10-2024 string<br><br><br>const formattedDateTwo = moment().format(&#8220;YYYY-MM-DD&#8221;);<br>console.log(formattedDateTwo, typeof formattedDateTwo);<br>\/\/ 2024-10-03 string<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Manipulating dates<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>const nextWeek = moment().add(7, &#8220;days&#8221;);<br>console.log(nextWeek, typeof nextWeek);<br>\/\/ Moment&lt;2024-10-10T09:13:40+05:30&gt; object<br><br>const lastMonth = moment().subtract(1, &#8220;months&#8221;);<br>console.log(lastMonth, typeof lastMonth);<br>\/\/ Moment&lt;2024-09-03T09:13:40+05:30&gt; object<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Validation and Relative time<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>const isValidDate = moment(&#8220;2024-02-30&#8221;).isValid();<br>console.log(isValidDate); \/\/ false<br><br>const relativeTime = moment(&#8220;2020-01-01&#8221;).fromNow();<br>console.log(relativeTime, typeof relativeTime);<br>\/\/ 5 years ago string<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Chaining Methods<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>const now = moment();<br>console.log(now);<br>\/\/ Moment&lt;2024-10-03T09:26:07+05:30&gt; object<br><br>const resultOne = moment().add(5, &#8220;days&#8221;).subtract(2, &#8220;months&#8221;);<br>console.log(resultOne, typeof resultOne);<br>\/\/ Moment&lt;2024-08-08T09:24:59+05:30&gt; object<br><br>const resultTwo = moment()<br>&nbsp; .add(5, &#8220;days&#8221;)<br>&nbsp; .subtract(2, &#8220;months&#8221;)<br>&nbsp; .format(&#8220;DD-MM-YYY&#8221;);<br><br>console.log(resultTwo, typeof resultTwo);<br>\/\/ 08-08-242024 string<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><em>In case, you want to learn more about &#8220;dates in JavaScript&#8221; and gain in-depth knowledge on full-stack development, consider enrolling for HCL GUVI&#8217;s certified <a href=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=dates-in-JavaScript\" target=\"_blank\" rel=\"noreferrer noopener\">Full Stack Development Course<\/a> that teaches you everything from scratch and make sure you master it!<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>Although the built-in JavaScript Date object is compact and a component of the language, Moment.js offers more flexible and user-friendly functionality. In applications where complex date manipulation, formatting, and time zone management are necessary, Moment.js can greatly reduce development time and improve the readability of code. Though Moment.js offers advantages to current codebases, it&#8217;s crucial to remember that as of September 2020, it is no longer actively supported. As such, developers should take into account more contemporary options, such as date-fns or Luxon, for new projects.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Working with dates and times is a fundamental part of many JavaScript applications, from scheduling events and logging activities to displaying timestamps and performing date calculations. JavaScript provides the Date object as a built-in tool for creating, manipulating, and formatting dates and times. In this article, we\u2019ll take a comprehensive look at working with dates [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":67956,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[429,37],"tags":[],"views":"3031","authorinfo":{"name":"Shiva Sunchu","url":"https:\/\/www.guvi.in\/blog\/author\/shiva-sunchu\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/11\/Dates-in-JavaScript-300x112.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/67844"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=67844"}],"version-history":[{"count":6,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/67844\/revisions"}],"predecessor-version":[{"id":88230,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/67844\/revisions\/88230"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/67956"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=67844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=67844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=67844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}