JavaScript Date: Epoch Milliseconds and Instants
Quick answer
A Date represents a time value measured in milliseconds relative to 1970-01-01T00:00:00Z, or an invalid time value. It does not store a separate time zone.
How it works
new Date(ms) creates a date from a millisecond count. getTime() and valueOf() read the time value. Date.now() reads the current wall-clock time, which is not a monotonic elapsed timer. toISOString() formats a valid time in UTC and throws RangeError for invalid dates. Negative timestamps precede the epoch; finite values outside ±8.64e15 milliseconds are invalid.
const d=new Date(1000); console.log(d.toISOString(), d.getTime());Expected output in Node.js:
1970-01-01T00:00:01.000Z 10001000 milliseconds equals one second.
const d=new Date(-1); console.log(d.toISOString(), d.valueOf());Expected output in Node.js:
1969-12-31T23:59:59.999Z -1A negative count represents an instant before the epoch.
Common mistakes
Do not pass Unix seconds directly to a Date constructor expecting milliseconds. Multiplying by 1000 is required when the input contract is seconds.
Practice
Convert integer seconds near the Unix epoch to an ISO timestamp.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Date: Epoch Milliseconds and Instants — Knowledge Check
Apply the taught concept and predict the output.
What unit does Date.getTime use?
Does a Date instance store its own named time zone?
Why is Date.now not ideal for measuring elapsed execution time?
What is the exact console output in JavaScript Date: Epoch Milliseconds and Instants example 1?
const d=new Date(1000); console.log(d.toISOString(), d.getTime());What is the exact console output in JavaScript Date: Epoch Milliseconds and Instants example 2?
const d=new Date(-1); console.log(d.toISOString(), d.valueOf());