JavaScript for impatient programmers (beta)
Please support this book: buy it or donate
(Ad, please don’t block.)

41 Dates (Date)



This chapter describes JavaScript’s API for working with dates – the class Date.

41.1 Best practice: don’t use the current built-in API

The JavaScript Date API is cumbersome to use. Hence, it’s best to rely on a library for anything related to dates. Popular libraries include:

Consult the blog post “Why you shouldn’t use Moment.js…” for the pros and cons of these libraries.

Additionally, TC39 is working on a new date API for JavaScript: temporal.

41.1.1 Things to look for in a date library

Two things are important to keep in mind:

41.2 Time zones

41.2.1 Background: UTC vs. GMT

UTC (Coordinated Universal Time) and GMT (Greenwich Mean Time) are ways of specifying time that are similar, but subtly different:

Source: “The Difference Between GMT and UTC” at TimeAndDate.com

41.2.2 Dates only support UTC and the local time zone

You can’t specify arbitrary time zones for JavaScript dates. Two time systems are used:

Instances of Date store their time in UTC. Whenever you convert between dates and other data, you need to be mindful of whether UTC or the local time zone is used. For example: new Date() uses the local time zone, while .toISOString() uses UTC.

> new Date(2077, 0, 27).toISOString()
'2077-01-26T23:00:00.000Z'

Dates interpret 0 as January. The day of the month is 27 in the local time zone, but 26 in UTC.

  Date operations: UTC vs. local time zone

For each operation documented in this chapter, that converts between dates and other data, it is noted whether UTC or the local time zone is used.

41.2.3 The downsides of not being able to specify time zones

Not being able to specify time zones, has two downsides:

41.3 Background: date time formats (ISO)

Date time formats describe:

The following is an example of a date time string returned by .toISOString():

'2033-05-28T15:59:59.123Z'

Date time formats have the following structures:

Alternative to Z – time zones relative to UTC:

41.4 Time values

A time value represents a date via the number of milliseconds since 1 January 1970 00:00:00 UTC.

Time values can be used to create Dates:

const timeValue = 0;
assert.equal(
  new Date(timeValue).toISOString(),
  '1970-01-01T00:00:00.000Z');

Coercing a Date to a number returns its time value:

> Number(new Date(123))
123

Ordering operators coerce their operands to numbers. Therefore, you can use these operators to compare Dates:

assert.equal(new Date('1972-05-03') < new Date('2001-12-23'), true);
// Internally:
assert.equal(73699200000 < 1009065600000, true);

41.4.1 Creating time values

The following methods create time values:

41.4.2 Getting and setting time values

41.5 Creating Dates

41.5.1 Pitfalls of the first constructor

The first variant of new Date() receives two time units that are problematic:

41.6 Getters and setters

41.6.1 Time unit getters and setters

Dates have getters and setters for time units. For example:

These getters and setters conform to the following patterns:

These are the time units that are supported:

There is one more getter that doesn’t conform to the previously mentioned patterns:

41.7 Converting Dates to strings

Example Date:

const d = new Date(0);

41.7.1 Strings with times

41.7.2 Strings with dates

41.7.3 Strings with dates and times

41.7.4 Other methods

The following three methods are not really part of ECMAScript, but rather of the ECMAScript internationalization API. That API has significant functionality for formatting dates (incl. support for time zones), but not for parsing them.

  Exercise: Creating a date string

exercises/dates/create_date_string_test.mjs