A Guide to Working with Dates in JavaScript

JavaScript provides a built-in Date constructor to work with dates and times. It represents time as the number of milliseconds elapsed since the epoch, January 1, 1970, 12:00 UTC.

Creating a date

The Date() constructor returns a date object, and by default, it will be created with the current date and time. For instance,

javascript
1const today = new Date();
2
3console.log(today);
text
12024-05-20T19:15:05.743Z

You can also create a custom date by passing a date string.

javascript
1const date = new Date("2022-03-25");
2
3console.log(date);
text
12022-03-25T00:00:00.000Z

Date formatting

The Date() constructor accepts three types of date strings: ISO date, short date, and long date. Our previous example uses an incomplete ISO format date string. The full ISO date looks like this:

Wait, there is more!

You need an account to access the rest of this lesson. Our course is 50% off for limited time only! Don't miss the opportunity!

🎉 Create an Account 🎉