Things I learned about JavaScript’s Date

JavaScript’s Date does not stop to amaze me.
Probably every developer has encountered lots of problems when working with it.
Most projects use other date libraries like Moment.js or JS-Joda to cope with its shortcomings.
In case you want to or have to stick to JavaScript’s Date here are some things I find useful to remember.

A Date object can be created several ways. Here are the most popular ones:

const d1 = new Date(1692967999168);
const d2 = new Date('2023-08-25T12:53:19.168Z');
const d3 = new Date(2023, 7, 25, 12, 53, 19, 168);Code-Sprache: JavaScript (javascript)

Every created date refers to the same date in time:

[d1, d2, d3].map(d => d.toISOString())
// results in
[
    "2023-08-25T12:53:19.168Z",
    "2023-08-25T12:53:19.168Z",
    "2023-08-25T10:53:19.168Z"
]Code-Sprache: JavaScript (javascript)

Why did I use toISOString and not toString?

The answer is that toISOString always uses the UTC time-zone and toString uses your browser’s local time-zone.

If you want to compare dates always use toISOString.

This means that a date object is always both: a local date and an UTC-date.

In case you want to retrieve the UTC-related information from the date object, Date provides certain functions for that: getUTCHours, getUTCMonth and so on.

If you want the same information for your browser’s local time-zone you may use getHours, getMonth and so on.

Unfortunately, there is no way (so far) to easily retrieve the same information for a time-zone that is neither UTC nor your browser’s time-zone.

However, if you just want to format the date in a certain time-zone, that can be done using the Intl.DateTimeFormat:

console.log(
  new Intl.DateTimeFormat('en-GB', { dateStyle: 'full', timeStyle: 'long', timeZone: 'Australia/Sydney' }).format(new Date(1692967999168)),
);
// result: Friday, 25 August 2023 at 22:53:19 GMT+10Code-Sprache: JavaScript (javascript)

Use Intl.DateTimeFormat to format a date with a certain time-zone.

Sometimes you may encounter invalid date-objects. That may happen on purpose or by accident (e.g. an invalid user input).

An invalid date-object is still an object of type Date but you may run into errors if you try to do something useful with it:

new Date('13-13-13').toISOString();
// will throw: Uncaught RangeError: Invalid time value at Date.toISOStringCode-Sprache: JavaScript (javascript)

Fortunately, there is an easy way to detect an invalid date. Calling getTime on an invalid date will return NaN (not a number):

const invalidDate = new Date('13-13-13');
const timestamp = invalidDate.getTime();
Number.isNaN(timestamp); // evaluates to true!Code-Sprache: JavaScript (javascript)

getTime will evaluate to Number.NaN if the date is invalid.

This implies that you can create an invalid date with new Date(Number.NaN).

Well folks, that’s it so far. I hope you can use some of the provided insights.

Let me know if you find a bug or want something to be added.

Until then, happy coding!