On top of different date-time formats, you have to consider timezone and local differences.
This date-time combination is known as the epoch time.
As far as JavaScript is concerned, its the beginning of time in the world.

It’s free, every week, in your inbox.
you’re able to simply create a date usingnew Date().
you might pass parameters to the Date constructor to create a date of your choice.

The given parameter can take different forms.
The format used in these strings is the ISO 8601 calendar extended format.
you might refer to its details in theECMAScript specification.

Pass date arguments
you might 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.
When we inspect the created dates outcome, we can notice one crucial difference in the final date.

However, when we print the result, the month is October instead.
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.
In this method of creating a date, we cant pass an argument to indicate its time zone.
So, its defaulted to the local time of the system.

But we can use theDate.UTCfunction to convert the date to UTC before passing it to the Date constructor.
We can pass this elapsed time value, called a timestamp, to indicate the date we are creating.
you’re free to also use theDate.now()function for the same task.

Formatting dates
JavaScript provides several built-in functions to format a date.
However, these functions only convert the date to a format specific to each one.
Lets see how each formatting function works.

Internationalization API
ECMAScriptInternationalization APIallows the formatting of a date into a specific locale using the Intl object.
Now, you’ve got the option to convert the date to a custom format using retrieved parts.
Updating dates
JavaScript provides several methods to edit an already created date.

In this section, Ill only talk about popular multi-purpose libraries.
Moment.js used to be the king of date manipulation libraries among JavaScript developers.
However,its developers recently announcedthat its 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.
Its dubbed as Lodash for dates due to its versatility.
Similar to Date-fns, Luxon offers data formatting and parsing functions.

Also, it has native Intl support and is chainable.
Working with dates is always painful in almost (if not all) programming languages.
Sign up for updates on everything related to programming, AI, and computer science in general.

Also tagged with




