Sub topic 18
Serializing and Storing Temporal Values
This chapter assumes you can create Temporal values with from(), know the difference between Instant, ZonedDateTime, and the Plain types, and have used JSON.stringify and JSON.parse. The examples import the polyfill. If your runtime has native Temporal, you can remove the import.
How Temporal Values Become JSON
Every Temporal type has a toJSON() method that returns the same string as toString(). That string is in ISO 8601 format, with bracketed extensions for time zone and calendar. JSON.stringify calls toJSON() automatically. The reverse step does not happen on its own: JSON.parse gives you back a plain string, and you must pass that string to the correct type's from() method.
import { Temporal } from '@js-temporal/polyfill';
const record = {
due: Temporal.PlainDate.from('2024-03-15'),
createdAt: Temporal.Instant.from('2024-03-01T09:30:00Z'),
meeting: Temporal.ZonedDateTime.from('2024-03-20T10:00[Europe/Paris]'),
};
const json = JSON.stringify(record);
console.log(json);
// {"due":"2024-03-15","createdAt":"2024-03-01T09:30:00Z",
// "meeting":"2024-03-20T10:00:00+01:00[Europe/Paris]"}
const types = {
due: Temporal.PlainDate,
createdAt: Temporal.Instant,
meeting: Temporal.ZonedDateTime,
};
const restored = JSON.parse(json, (key, value) =>
key in types ? types[key].from(value) : value
);
console.log(restored.meeting.timeZoneId); // 'Europe/Paris'
console.log(restored.due.equals(record.due)); // trueA reviver function is a callback passed to JSON.parse that can replace each value as it is parsed. The reviver above uses a map from field name to type. The string alone does not tell you which type it came from, so your schema has to record that information.
Choosing What to Store
| Requirement | Store as | Example string |
|---|---|---|
| When something happened | `Instant` | `2024-03-01T09:30:00Z` |
| Birthday or due date | `PlainDate` | `2024-03-15` |
| Future local event | `ZonedDateTime` | `2024-03-20T10:00:00+01:00[Europe/Paris]` |
Using Non-ISO Calendars
By default, Temporal uses the ISO 8601 calendar. Plain types and ZonedDateTime can also carry a different calendar, such as hebrew, japanese, or islamic-umalqura. Use withCalendar() to view the same day in another calendar.
import { Temporal } from '@js-temporal/polyfill';
const iso = Temporal.PlainDate.from('2024-03-15');
const hebrew = iso.withCalendar('hebrew');
console.log(hebrew.year); // 5784
console.log(hebrew.monthCode); // 'M06' (Adar II in a leap year)
console.log(hebrew.day); // 5
console.log(hebrew.toString()); // '2024-03-15[u-ca=hebrew]'
const back = Temporal.PlainDate.from(hebrew.toString());
console.log(back.calendarId); // 'hebrew'
console.log(back.equals(hebrew)); // trueThe [u-ca=hebrew] annotation is part of the serialized string, so from() restores the calendar along with the date. Month numbers differ between calendars and between years: a Hebrew leap year has 13 months. Use monthCode rather than month when you need a stable identifier for a month.
Exercise
You serialize Temporal.PlainDate.from('2024-03-15').withCalendar('japanese') and later parse it with Temporal.PlainDate.from(). Which calendar does the result use, and why?
Show answer
It uses the japanese calendar. toString() adds the [u-ca=japanese] annotation to the ISO string, and from() reads that annotation when it rebuilds the date.
Exercise
Which type should you store for a user's recurring 9:00 alarm in their local time zone, which applies to every day and not to one moment?
Show answer
Store a PlainTime (for example '09:00:00') together with the user's IANA time zone ID. The alarm describes a wall-clock time, not one exact instant.