Timestamps
Measure elapsed time or store event instants using epoch milliseconds.
JavaScript · Class
The JavaScript Date class represents a single instant as milliseconds from the UTC epoch. It offers constructors, static timestamp utilities, and local or UTC accessors and formatters.
A Date stores one instant as an integral millisecond timestamp relative to 1970-01-01T00:00:00Z. Its accessors and mutators can interpret or change that instant in either local time or UTC, while formatting methods produce strings.
new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
Date()A Date object represents an instant in time; the Date() function form returns a current date-time string.
const epoch = new Date(0);
const now = new Date();
console.log(epoch.toISOString());
console.log(now.getTime());
console.log(Date.now());Creates dates, formats the epoch in UTC, and reads current timestamps.
Measure elapsed time or store event instants using epoch milliseconds.
Read calendar fields according to the runtime's local time zone.
Read or format an instant consistently in UTC.
Convert supported date strings into timestamps.
A Date represents one instant using milliseconds from the UTC epoch, 1970-01-01T00:00:00Z. The timestamp itself is independent of time zone.
Follow-up: How do you detect an invalid Date? Use Number.isNaN(date.getTime()), because invalid dates have a NaN timestamp.
new Date() constructs a Date object for the current instant. Date() without new instead returns a string for the current date and time, and supplied arguments to the function form are ignored.
Follow-up: What does new Date(0) represent? It represents the epoch instant, midnight at the beginning of January 1, 1970 UTC.
Methods beginning with getUTC interpret calendar fields in universal time, while methods such as getDate() interpret them in local time. Both operate on the same underlying timestamp.
Follow-up: Why can local calendar fields differ from UTC fields? The host environment's local time zone may place the instant on a different date or clock time.
Date.now()Date.parse()Date.UTC()Date.prototype.getTime()Date.prototype.setTime()Date.prototype.toISOString()Date.prototype.getUTCFullYear()