Sub topic 1
Why Temporal Exists: What Goes Wrong with Date
JavaScript's Date object was modeled on Java's java.util.Date in 1995. Java replaced its version years ago, but JavaScript kept Date for compatibility. Temporal is the replacement. It is a new global namespace with separate, immutable types for different kinds of date and time values. This chapter covers the specific ways Date causes bugs, so you can see what problem each Temporal design choice solves.
Prerequisites
This chapter assumes you:
- Can read modern JavaScript (ES2015+):
const, arrow functions, template literals. - Understand objects, methods, references, and exceptions.
- Have created and read
Dateobjects before, for example withnew Date()andgetFullYear(). - Can run snippets in Node.js or a browser console.
None of the code in this chapter uses Temporal yet, so you don't need a polyfill.
One Type for Every Job
A Date holds exactly one thing: a count of milliseconds since 1970-01-01T00:00:00Z. Everything else, such as year, month, hour, and weekday, is computed from that number using either UTC or the host machine's local time zone.
That one model is used for very different requirements:
- A birthday (a calendar date with no time and no time zone).
- A store's opening time (a wall-clock time that applies every day).
- A server log timestamp (an exact moment).
- A meeting in Tokyo (a moment tied to a specific time zone).
A birthday stored as a Date is really "midnight at some instant". When the code runs in a different time zone, it can show up as the previous day.
Mutability and Shared References
Date objects are mutable. Every set* method changes the object in place, and every variable that holds a reference to it sees the change.
function addOneDay(date) {
date.setDate(date.getDate() + 1);
return date;
}
const dueDate = new Date(2024, 2, 15); // March 15, 2024, local time
const reminder = addOneDay(dueDate);
console.log(reminder === dueDate); // true: same object
console.log(dueDate.getDate()); // 16: the original was changedThe usual defense is to copy first with new Date(date.getTime()). Every function that receives a Date has to remember to do that, and one that forgets causes a bug far from where the value was created.
Zero-Based Months and Silent Rollover
Months are numbered 0 to 11, while days are numbered 1 to 31. new Date(2024, 3, 15) is April 15, not March 15.
Out-of-range values are accepted without complaint and roll over into the next unit:
const d = new Date(2024, 0, 31); // January 31, 2024
d.setMonth(1); // ask for "February 31"
console.log(d.getMonth()); // 2 (March)
console.log(d.getDate()); // 2A "plus one month" operation on January 31 gave March 2. Nothing signals that the requested date didn't exist, so you get neither a clamped date (February 29) nor an error.
Inconsistent String Parsing
Date parses ISO 8601 strings differently depending on their shape:
const dateOnly = new Date('2024-03-15');
const dateTime = new Date('2024-03-15T00:00');
console.log(dateOnly.toISOString());
// '2024-03-15T00:00:00.000Z' everywhere: date-only forms are read as UTC
console.log(dateTime.toISOString());
// Depends on the machine: date-time forms without an offset are read as local time.
// In New York this prints '2024-03-15T04:00:00.000Z'Calling dateOnly.getDate() in any time zone west of UTC returns 14. Strings that aren't ISO 8601, such as '03/15/2024' or 'March 15, 2024', are parsed by implementation-specific rules. They may work in one engine and return Invalid Date in another.
Only Two Time Zones
A Date can be read in the host's local zone or in UTC, and nowhere else. You can display an instant in another zone through Intl:
const instant = new Date('2024-03-15T12:00:00Z');
console.log(instant.toLocaleString('en-US', { timeZone: 'Asia/Tokyo' }));
// '3/15/2024, 9:00:00 PM'You can't do arithmetic in Tokyo time, though. There is no method like "add one day in Asia/Tokyo" or "get the hour in Europe/Paris" that returns a number. Code that needs this usually ends up parsing formatted strings or pulling in a third-party library.
Daylight Saving Arithmetic
Developers commonly add a day by adding 24 * 60 * 60 * 1000 milliseconds. On a day when the local zone moves its clocks, a calendar day is 23 or 25 hours long, so the result lands an hour off. setDate(getDate() + 1) keeps the wall-clock time, but only for the host's local zone, so the same code gives different results on a server in UTC and a laptop in New York. Date has no way to say which of these two meanings you want.
Equality and Missing Concepts
Two Date objects for the same moment are not equal under == or ===, because both operators compare object identity:
const a = new Date(0);
const b = new Date(0);
console.log(a === b); // false
console.log(a.getTime() === b.getTime()); // true
console.log(a < new Date(1)); // true, via valueOf coercion< works because of numeric coercion, which makes == failing all the more surprising. Date is also missing several concepts entirely:
- There is no duration type, so "1 hour 30 minutes" has to be a bare number of milliseconds.
- Precision stops at milliseconds, so it can't hold the nanosecond timestamps that databases and other systems produce.
- Only the Gregorian calendar is supported, so Hebrew, Islamic, or Japanese calendar dates can be formatted but not computed with.
How Temporal Responds
| Problem with Date | Temporal's approach | Covered in |
|---|---|---|
| One type for everything | Separate types for exact, wall-clock, and zoned values | A Map of the Temporal Types |
| Mutable objects | Every operation returns a new object | Immutable by Design |
| Zero-based months | Months numbered 1 to 12 | Plain Dates and Times |
| Silent rollover | `overflow: 'constrain'` or `'reject'` | Handling Invalid Results |
| Loose parsing | Strict ISO 8601 / RFC 9557 parsing that throws on bad input | Creating and Parsing |
| Local and UTC only | Any IANA time zone, with arithmetic | ZonedDateTime |
| Ambiguous DST math | Calendar units vs exact units, plus `disambiguation` | Daylight Saving Time |
| No durations | `Temporal.Duration` | Working with Temporal.Duration |
| Identity equality | `equals()` and static `compare()` | Comparing and Sorting |
| Millisecond limit | Nanosecond precision | Exact Moments with Instant |
Exercise
A form stores a user's birthday with new Date('1990-07-04'), and the profile page shows it with getDate(). A user in Los Angeles sees "3" instead of "4". Explain why.
Show answer
A date-only ISO string is parsed as UTC midnight, 1990-07-04T00:00Z. In Los Angeles (UTC-7 in July) that instant is 17:00 on July 3, so the local getDate() returns 3. The bug comes from storing a calendar date as an exact instant.
Exercise
What does this code log, and why? const d = new Date(2023, 1, 29); console.log(d.getMonth(), d.getDate());
Show answer
It logs 2 1. Month index 1 is February, and 2023 is not a leap year, so "February 29" rolls over silently to March 1. Month index 2 is March.