Sub topic 6
Creating and Parsing Temporal Values
This chapter assumes you can write modern JavaScript, handle exceptions with try/catch, and have a working Temporal global, either native or from the polyfill set up earlier. It also assumes you know the Plain types and Temporal.Instant from the previous chapters.
Three ways in
Every Temporal type offers the same entry points:
| Entry point | Input | Typical use |
|---|---|---|
| `Type.from(string)` | ISO 8601 / RFC 9557 string | Parsing API data, form input, stored values |
| `Type.from(bag)` | Object with named fields | Building values from separate parts |
| `new Type(...)` | Positional numeric arguments | Low-level, fully specified values |
const date = Temporal.PlainDate.from('2024-03-15');
const sameDate = new Temporal.PlainDate(2024, 3, 15);
const fromBag = Temporal.PlainDate.from({ year: 2024, month: 3, day: 15 });
console.log(date.equals(sameDate)); // true
console.log(fromBag.toString()); // '2024-03-15'
console.log(date.dayOfWeek); // 5 (Friday; Monday is 1)Months start at 1. If you are coming from new Date(2024, 2, 15), this change causes the most off-by-one bugs.
What each type accepts from a string
Each type reads only the parts of a string that it needs. Some types refuse strings that leave out information they require.
// Extra parts are ignored for Plain types
console.log(Temporal.PlainDate.from('2024-03-15T10:30:00').toString()); // '2024-03-15'
console.log(Temporal.PlainTime.from('09:30').toString()); // '09:30:00'
// Instant needs an exact point in time, so an offset or Z is required
console.log(Temporal.Instant.from('2024-03-15T12:00:00Z').toString()); // '2024-03-15T12:00:00Z'
try {
Temporal.Instant.from('2024-03-15T12:00:00');
} catch (e) {
console.log(e instanceof RangeError); // true
}
// ZonedDateTime needs a bracketed IANA time zone
const zdt = Temporal.ZonedDateTime.from('2024-03-15T12:00[America/New_York]');
console.log(zdt.offset); // '-04:00'Strings reject, bags can constrain
An invalid date in a string always throws. A property bag passed to from() defaults to overflow: 'constrain', which clamps out-of-range fields. Constructors always throw on invalid input.
try {
Temporal.PlainDate.from('2024-02-30');
} catch (e) {
console.log('string:', e.constructor.name); // 'string: RangeError'
}
console.log(Temporal.PlainDate.from({ year: 2024, month: 2, day: 30 }).toString());
// '2024-02-29'
try {
new Temporal.PlainDate(2024, 2, 30);
} catch (e) {
console.log('constructor:', e.constructor.name); // 'constructor: RangeError'
}Common errors
- Passing a
Dateobject tofrom(). It throws aTypeError. Convert it through epoch milliseconds instead (covered later). - Calling
Temporal.Instant.from()with a local time string that has no offset. - Forgetting that
new Temporal.Instant()takes epoch nanoseconds as aBigInt. UseTemporal.Instant.fromEpochMilliseconds(ms)for millisecond values. - Using zero-based months in a property bag or constructor.
Exercise
What does each line print or throw? (1) Temporal.PlainDate.from({ year: 2023, month: 4, day: 31 }).toString() (2) Temporal.PlainDate.from('2023-04-31') (3) Temporal.Instant.from('2023-04-30T08:00+02:00').toString()
Show answer
(1) '2023-04-30', because the bag is constrained to the last day of April. (2) It throws a RangeError, because strings are always rejected when invalid. (3) '2023-04-30T06:00:00Z', because the offset is applied and the instant is shown in UTC.
Exercise
You receive the field "2024-07-01T09:00" from a form and need a ZonedDateTime in Europe/Berlin. Write one line that creates it.
Show answer
Temporal.PlainDateTime.from('2024-07-01T09:00').toZonedDateTime('Europe/Berlin'), or Temporal.ZonedDateTime.from('2024-07-01T09:00[Europe/Berlin]').