Skip to content

Time and Dates

A Date object holds a single moment in time, stored as the number of milliseconds since midnight UTC on 1 January 1970, which is called the Unix epoch. The moment is the same everywhere, but most Date methods read and write it in the local time zone of the computer running the code, so the same program can print different results in different places.

The output on this page was produced with the time zone set to Europe/London (TZ=Europe/London node example.cjs). London is on GMT, which matches UTC, until the clocks go forward on 30 March 2025, and on BST, one hour ahead of UTC, from then until 26 October 2025.

new Date() with no arguments gives the current moment. The other forms take an ISO 8601 string, separate parts, or a number of milliseconds since the epoch.

const launch = new Date('2025-03-14T09:30:00Z');
const fromParts = new Date(2025, 2, 14, 9, 30);
const fromMs = new Date(0);
console.log(launch.toISOString());
console.log(fromParts.toISOString());
console.log(fromMs.toISOString());

This prints:

2025-03-14T09:30:00.000Z
2025-03-14T09:30:00.000Z
1970-01-01T00:00:00.000Z

Two details catch people out with the separate-parts form. Months count from 0, so 2 is March, while days of the month count from 1. The parts are also read as local time, so fromParts matches launch only because London was on UTC that day.

The only string format that the specification requires every engine to read is a simplified form of ISO 8601, such as 2025-07-01 or 2025-07-01T14:00:00Z. Even within it, a date on its own is read as UTC, while a date and time without a Z or offset is read as local time.

console.log(new Date('2025-07-01').toISOString());
console.log(new Date('2025-07-01T00:00').toISOString());
console.log(new Date('01/07/2025').toISOString());
console.log(new Date('July 1st, 2025').getTime());

This prints:

2025-07-01T00:00:00.000Z
2025-06-30T23:00:00.000Z
2025-01-07T00:00:00.000Z
NaN

The second line is midnight in London, which is 23:00 UTC the day before, because London was on BST. The third line shows that a slashed date is read in the American order, month first, so 01/07/2025 becomes 7 January. Any other format depends on the engine, and the last one fails, so an invalid date’s getTime() returns NaN. Write dates in ISO format, with a Z or an offset whenever the time matters.

The get methods return one part of the date in local time, and each has a getUTC twin that returns it in UTC. getDay returns the day of the week, from 0 for Sunday to 6 for Saturday, and getDate returns the day of the month.

const d = new Date('2025-03-14T09:30:00Z');
console.log(d.getFullYear(), d.getMonth(), d.getDate());
console.log(d.getDay());
console.log(d.getHours(), d.getMinutes());
console.log(d.getUTCHours());

This prints:

2025 2 14
5
9 30
9

In summer, the local and UTC hours differ. getTimezoneOffset returns the difference in minutes, and its sign is the reverse of what you might expect, because it is UTC minus local time.

const summer = new Date('2025-07-01T09:30:00Z');
console.log(summer.getHours());
console.log(summer.getUTCHours());
console.log(summer.getTimezoneOffset());

This prints:

10
9
-60

Each get method has a matching set method, such as setFullYear, setMonth, setDate and setHours, which changes the date object in place. A value out of range rolls over into the next unit, which makes date arithmetic easy. Day 0 of a month means the last day of the month before.

const due = new Date('2025-03-14T09:30:00Z');
due.setDate(due.getDate() + 30);
console.log(due.toISOString());
due.setMonth(due.getMonth() + 1, 0);
console.log(due.toISOString());

This prints:

2025-04-13T08:30:00.000Z
2025-04-30T08:30:00.000Z

Thirty days on from 14 March is 13 April. The UTC time has moved from 09:30 to 08:30 because setDate works in local time, so the result is still 09:30 in London, now on BST. The second call moves to day 0 of May, which is 30 April. Use the setUTC methods when a fixed number of hours matters more than the local clock time.

toISOString and toJSON give a fixed UTC format, which suits storage and sending data. toString and toDateString give an English description in local time. For anything shown to a user, toLocaleDateString, toLocaleTimeString and toLocaleString format the date for a locale, and accept options such as a time zone.

const d = new Date('2025-03-14T09:30:00Z');
console.log(d.toISOString());
console.log(d.toJSON());
console.log(d.toUTCString());
console.log(d.toString());
console.log(d.toDateString());
console.log(d.toLocaleDateString('en-GB'));
console.log(d.toLocaleString('en-US'));
console.log(d.toLocaleDateString('en-GB', { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' }));
console.log(d.toLocaleTimeString('en-GB', { timeZone: 'Asia/Tokyo', timeZoneName: 'short' }));

This prints:

2025-03-14T09:30:00.000Z
2025-03-14T09:30:00.000Z
Fri, 14 Mar 2025 09:30:00 GMT
Fri Mar 14 2025 09:30:00 GMT+0000 (Greenwich Mean Time)
Fri Mar 14 2025
14/03/2025
3/14/2025, 9:30:00 AM
Friday, 14 March 2025
18:30:00 GMT+9

Only the first three lines are the same in every time zone. Leaving out the locale argument uses the user’s own locale, which is usually what a web page wants. To format many dates the same way, create one Intl.DateTimeFormat and reuse it.

Date has well-known weaknesses: objects that change in place, months counted from 0, loose parsing and no support for time zones other than local and UTC. Temporal is a newer built-in API that fixes these, with separate types for a calendar date, a wall-clock time and a moment in a named time zone. It is shipping in browsers but is not yet available everywhere, including Node 25 by default, so check MDN’s Temporal page for current support before relying on it.

Timestamps, Date.now() and valueOf() covers dates as numbers of milliseconds.

Steve Griffith covers this topic in his video Working with Time and Dates in JavaScript.