Date and Time in JavaScript

javascript
Back

Below are widely used date/time standards/systems(ways of tracking time).

Greenwich Mean Time (GMT) is often interchanged or confused with Coordinated Universal Time (UTC). But GMT is a time zone and UTC is a time standard.

In JavaScript's Date object, the time value is UTC.

new Date(); // Thu Jul 07 2021 18:51:31 GMT+0530 (India Standard Time)

A JavaScript date is specified as the number of milliseconds that have elapsed since midnight on January 1, 1970, UTC.

new Date().getTime(); // 1625750552245
// OR
Date.now(); // 1625750552245

This date and time are not the same as the UNIX epoch. In UNIX epoch date is specified as the number of seconds that have elapsed since midnight on January 1, 1970, UTC)

Also we have to consider Time zones and time offsets

To understand date parts like GMT+0530 from above date, keep reading below

ISO 8601

Why to choose ISO 8601

The International Standard for the representation of dates and times is ISO 8601.

The ISO 8601 standard is more modern. The formats are wisely designed to be easy to parse by machine as well as easy to read by humans across cultures.

ISO 8601 date format breakdown/parts

Let's take example of date/time in ISO 8601 standard. To get date in ISO, try new Date().toISOString()

new Date().toISOString(); // "2021-07-07T07:20:04.633Z"

Here is format

// Complete date plus hours, minutes and seconds
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
// Complete date plus hours, minutes, seconds
// and a decimal fraction of a second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)

In above example

where:

This profile defines two ways of handling time zone offsets:

Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z").

date/time conversions

So finally, with JavaScript's Date object we can covert our local(host machine) date/time in any standard like ISO, UTC etc

like

new Date().toLocaleString(); // "07/07/2021, 18:48:39"
new Date().toISOString(); // "2021-07-07T07:20:04.633Z"
new Date().toUTCString(); // "Wed, 07 Jul 2021 07:19:56 GMT"
new Date().toGMTString(); // "Wed, 07 Jul 2021 07:19:40 GMT"

For more details you can play with new Date() in console.

Thanks!

References

MIT © Mohan Dere.
-