The Temporal API: dates and times without the Date pitfalls
JavaScript’s Date is a timestamp with a confusing interface: months start at 0, objects are mutable, there is no date without a time and no time zone other than UTC and the browser’s own. A booking widget that shows “Tuesday 10:00 in Berlin” to a visitor in New York needs a library for that. Temporal is the new built-in API: immutable objects, separate types for dates, times and zoned date-times, and real time zone and calendar support.
Problem: A WordPress booking plugin shows appointment slots. The slots are stored in the site’s time zone (Europe/Berlin), the script computes them with Date, and around the daylight saving change in March the slots shift by an hour for visitors in other countries.
Solution: Pass the site’s time zone from PHP (wp_timezone_string()), represent slots as Temporal.ZonedDateTime in that zone, do date arithmetic with add() and with(), convert to the visitor’s zone with withTimeZone() for display, and load a polyfill where Temporal is not yet available.
The types
Temporal.PlainDate: a calendar date without time or zone (a birthday, an opening day).Temporal.PlainTime: a wall-clock time (opening at 09:00).Temporal.ZonedDateTime: an exact moment in a specific time zone, including the DST rules of that zone.Temporal.Instant: an exact moment without a zone, like a Unix timestamp.Temporal.Duration: a length of time (PT30M).
All objects are immutable: methods return new objects.
Slots in the site’s time zone
// window.haBooking = { timeZone: 'Europe/Berlin', open: '09:00', close: '17:00', slotMinutes: 30 }
const { timeZone, open, close, slotMinutes } = window.haBooking;
function haSlotsFor( dateString ) {
const day = Temporal.PlainDate.from( dateString ); // '2025-03-30'
let slot = day.toZonedDateTime( { timeZone, plainTime: Temporal.PlainTime.from( open ) } );
const end = day.toZonedDateTime( { timeZone, plainTime: Temporal.PlainTime.from( close ) } );
const slots = [];
while ( Temporal.ZonedDateTime.compare( slot, end ) < 0 ) {
slots.push( slot );
slot = slot.add( { minutes: slotMinutes } );
}
return slots;
}
On 30 March 2025 Berlin switched to summer time; toZonedDateTime() with the zone name gets 09:00 local time right on that day, and add( { minutes: 30 } ) counts real elapsed time. No manual offsets anywhere.
Show them to the visitor
const visitorZone = Temporal.Now.timeZoneId(); // e.g. 'America/New_York'
for ( const slot of haSlotsFor( '2025-03-30' ) ) {
const local = slot.withTimeZone( visitorZone );
console.log(
slot.toLocaleString( 'en', { hour: '2-digit', minute: '2-digit' } ), // 09:00 in Berlin
local.toLocaleString( 'en', { weekday: 'short', hour: '2-digit', minute: '2-digit', timeZoneName: 'short' } )
);
}
// send to the server as an ISO string with offset and zone
const payload = { start: slot.toString() }; // '2025-03-30T09:00:00+02:00[Europe/Berlin]'
On the server, PHP reads the part before the bracket with new DateTimeImmutable( substr( $start, 0, strpos( $start, '[' ) ) ) and converts it to the site zone with wp_timezone().
Pass the time zone from PHP
wp_add_inline_script( 'ha-booking', 'window.haBooking = ' . wp_json_encode( array(
'timeZone' => wp_timezone_string(), // 'Europe/Berlin', or '+02:00' if the site uses a manual offset
'open' => '09:00',
'close' => '17:00',
'slotMinutes' => 30,
) ) . ';', 'before' );
If the site is set to a UTC offset instead of a city in Settings → General, wp_timezone_string() returns an offset like +02:00, which has no DST rules. A city name is the better setting for any site that deals with appointments.
Polyfill
if ( ! ( 'Temporal' in globalThis ) ) {
const { Temporal } = await import( 'https://cdn.jsdelivr.net/npm/temporal-polyfill@0.3.0/+esm' );
globalThis.Temporal = Temporal;
}
Load it before the booking code, as a module (the top-level await requires one), or bundle a polyfill with the plugin’s build instead of a CDN.
NOTE: Temporal shipped in Firefox 139 (May 2025). Chrome, Edge and Safari do not have it yet (August 2025), so the polyfill is needed for most visitors for now. The PHP side of date handling in WordPress (wp_date(), current_time(), wp_timezone()) is unaffected and remains the source of truth for stored dates.