Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Dates and time

A Date represents an instant as milliseconds since the start of 1970 in UTC. Distinguish an instant, a local calendar representation, and a duration to avoid time-zone-related mistakes.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

new Date(milliseconds)
new Date("YYYY-MM-DDTHH:mm:ss.sssZ")
date.getTime()
date.toISOString()

Date stores an instant as an integer number of milliseconds since 1970-01-01T00:00:00.000Z; negative values represent earlier instants. It does not store a time zone. getTime() returns that number, while toISOString() represents the instant in UTC with a trailing Z.

An instant, a local date and time, and a duration are different concepts. Local methods interpret an instant in the environment's time zone, while UTC methods use UTC. Clock changes can make a local day differ from 24 hours. Adding 86400000 milliseconds therefore means 24 hours, not necessarily the same local time on the next day.

When parsing text, use the standard format with an explicit Z or offset. Without an offset, YYYY-MM-DD is interpreted as UTC, but a date-time is interpreted as local time. Other formats can depend on the implementation. An invalid Date has a time value of NaN; test it with Number.isNaN(date.getTime()). Calling toISOString() on that object throws RangeError.

The example adds 90 minutes to a fixed instant and calculates the difference in seconds. The operations shown belong to ECMAScript, not browser Web APIs.

Examples

const start = new Date("2024-01-01T00:00:00.000Z");
const end = new Date(start.getTime() + 90 * 60 * 1000);
console.log(end.toISOString());
console.log((end.getTime() - start.getTime()) / 1000);

Result

2024-01-01T01:30:00.000Z
5400

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources