Sub topic 1
What a Date Really Is: One Instant in Milliseconds
A JavaScript Date looks like it holds a year, a month, a day, and a time. It holds none of those. It holds a single number: the count of milliseconds since the Unix epoch. Once you understand that number, most of the strange behavior of Date starts to make sense.
Prerequisites
This chapter assumes you can:
- Write JavaScript variables, functions, and objects.
- Run code in a browser console or in Node.js.
- Explain in general terms what UTC and a time zone are.
The time value
The specification calls the number inside every Date its time value. It counts milliseconds from the Unix epoch, which is midnight UTC on January 1, 1970. A positive value is a moment after the epoch. A negative value is a moment before it.
const now = Date.now(); // milliseconds since 1970-01-01T00:00:00Z
const d = new Date(0);
console.log(d.toISOString()); // "1970-01-01T00:00:00.000Z"
console.log(d.getTime()); // 0
const a = new Date(2024, 0, 1);
const b = new Date(2024, 0, 2);
console.log(b - a); // 86400000 (one day, unless a DST change falls in between)b - a works because subtraction converts each Date to a number by calling valueOf(), and that returns the time value. getTime(), valueOf(), and Number(d) all give you the same number.
What the number does not contain
A time value names an instant, one point on the global timeline. It stores no time zone, no locale, and no calendar. When you print a date or call getHours(), JavaScript works out the wall-clock fields from the time value at that moment, using the time zone of the runtime or one you pass in. Later chapters cover those views. For now, remember that every human-readable form is computed from the number and is never stored.
There are two more facts worth knowing:
- Leap seconds are ignored. Every day is exactly 86,400,000 ms in a time value.
- The range is limited. Valid values run from -8.64e15 to 8.64e15 ms, which is about 273,000 years either side of 1970. Anything outside that range, or any failed construction, gives an Invalid Date, whose time value is
NaN.
console.log(new Date(8.64e15).toISOString()); // "+275760-09-13T00:00:00.000Z"
console.log(new Date(8.64e15 + 1).getTime()); // NaN
console.log(Number.isNaN(new Date("nope").getTime())); // true: how to detect an Invalid DateCommon errors
| Mistake | Why it happens | Fix |
|---|---|---|
| Treating a timestamp as seconds | Unix tools and many APIs use seconds | Multiply by 1000 before `new Date(...)` |
| Expecting a Date to "remember" its zone | The zone is never stored | Store or pass the zone separately |
| Missing an Invalid Date | It is still an object, so it is truthy | Check `Number.isNaN(d.getTime())` |
Exercise
What does new Date(86400000).toISOString() print, and why?
Show answer
"1970-01-02T00:00:00.000Z". 86,400,000 ms is exactly one day after the epoch, and toISOString always shows UTC.
Exercise
An API returns { "created": 1717200000 }. Write one line that turns it into a correct Date.
Show answer
const created = new Date(data.created * 1000); The value is in seconds, and the Date constructor expects milliseconds.