Sub topic 3
Comparing and Sorting Temporal Values
This chapter assumes you can already create Temporal values and do arithmetic with them. The examples use the global Temporal object, either built into the runtime or loaded from the polyfill.
Comparing and Sorting
Temporal objects throw a TypeError when you compare them with < or >. This blocks silent bugs caused by string or number coercion. For equality, use equals. For ordering, use the static compare method, which returns -1, 0, or 1. You can pass it straight to sort:
const dates = ['2024-05-01', '2023-12-25'].map((s) => Temporal.PlainDate.from(s));
dates.sort(Temporal.PlainDate.compare);
console.log(dates.map(String)); // ['2023-12-25', '2024-05-01']Daylight Saving Time and Ambiguous Times
Some wall-clock times happen twice in a day, and others never happen at all. In New York, 02:30 on 2024-03-10 never happened because clocks jumped from 02:00 to 03:00. The disambiguation option chooses what Temporal does with such a time:
const s = '2024-03-10T02:30[America/New_York]';
console.log(Temporal.ZonedDateTime.from(s).toString());
// '2024-03-10T03:30:00-04:00[America/New_York]' (default 'compatible')
console.log(Temporal.ZonedDateTime.from(s, { disambiguation: 'earlier' }).toString());
// '2024-03-10T01:30:00-05:00[America/New_York]'
// { disambiguation: 'reject' } throws a RangeErrorRounding
The round method takes a smallestUnit and returns a new value:
console.log(Temporal.PlainTime.from('14:37:52').round({ smallestUnit: 'minute' }).toString()); // '14:38:00'Conversions, Formatting, and Legacy Date
An Instant becomes a ZonedDateTime once you give it a time zone. From a ZonedDateTime you can take out the plain parts. To move between Date and Temporal, go through epoch milliseconds. For display, use toLocaleString, which takes the same options as Intl.DateTimeFormat.
const legacy = new Date('2024-03-15T12:00:00Z');
const instant = Temporal.Instant.fromEpochMilliseconds(legacy.getTime());
const tokyo = instant.toZonedDateTimeISO('Asia/Tokyo');
console.log(tokyo.toPlainDate().toString()); // '2024-03-15'
console.log(tokyo.toLocaleString('en-US', { timeStyle: 'short' })); // '9:00 PM'
console.log(new Date(instant.epochMilliseconds).toISOString()); // '2024-03-15T12:00:00.000Z'Serialization and Calendars
JSON.stringify calls toJSON, which produces the ISO string. To restore the value, pass that string to the matching from. JSON does not record which type a value came from, so your code has to know which type to rebuild.
Non-ISO calendars attach to a value with withCalendar:
const hebrew = Temporal.PlainDate.from('2024-03-15').withCalendar('hebrew');
console.log(hebrew.year); // 5784
console.log(hebrew.toString()); // '2024-03-15[u-ca=hebrew]'Exercise
Why does dates.sort((a, b) => a - b) fail on an array of PlainDate objects, and what should you write instead?
Show answer
Subtraction calls valueOf, and Temporal objects throw a TypeError from it. Use dates.sort(Temporal.PlainDate.compare).
Exercise
You parse '2024-03-10T02:30[America/New_York]' with { disambiguation: 'reject' }. What happens?
Show answer
A RangeError is thrown, because that wall-clock time does not exist in New York on that date.