ISPEAKCODE.COM

TALK · JAVASCRIPT MEETUP

Time Travel in JavaScript

A tour of the Temporal API: why Date keeps breaking, what date libraries cost you, and how Temporal fixes it. Every example in the talk runs live, right here, in your browser and your time zone.

37 SLIDES TEMPORAL API 8 LIVE EXAMPLES
Jump to the live examples
Prefer reading? The Temporal API article on Medium
Nabil presenting Time Travel in JavaScript at the JavaScript Meetup: the title slide is on the projector screen and around forty people sit in rows of green chairs.
On stage at the JavaScript Meetup
Nabil, microphone in hand, presenting beside the screen showing the Time Travel in JavaScript title slide, with attendees seated on green chairs to the right.
Mid-talk
01

TIME TRAVEL IN JAVASCRIPT

A talk about the Temporal API

02Meme: 'How can I check date and time on JavaScript?' A skeleton on a bench: 'I'll just wait here.'
03

PART 1

The pitfalls of Date

04

JavaScript's Date object is based on legacy Java APIs

05

Several well-known issues

06

Timezone bugs

07Tweet: 'Elon Musk: I'm putting people on Mars! Developers: Fantastic, more timezones to support.'
08

Adding days or handling daylight saving time are error-prone

09Meme from Back to the Future: 'Is daylight savings time a form of time travel?!'
10

PART 2

Existing solutions and their drawbacks

11

Libraries like Moment.js or date-fns to handle dates

12Drake meme: rejecting 'x % 2 !== 0', approving the is-odd npm package.
13

Libraries can add to bundle size

14Heaviest objects in the universe: the Sun, a neutron star, a black hole, and node_modules, which bends space the most.
15

Complex timezone handling

16Meme: date-time library authors having to implement yet another timezone change, crying.
17

Libraries still depend on the Date object under the hood

18Scooby-Doo unmasking meme: under the Moment.js mask is JS Date.
19

PART 3

Why the Temporal API

20

Part of the modern ECMAScript proposal

SINCE THE TALK Temporal reached Stage 4 in 2025.

21

Designed to handle date and time more intuitively

22

It's immutable, making it safer to use across your applications

23

Has built-in support for time zones and locales

24

PART 4

Capabilities of Temporal

25

Temporal separates concerns

26

Dates, times, date-times, time zones and durations are distinct types

PlainDatePlainTimePlainDateTimeZonedDateTimeInstantDuration
27

Add

TEMPORAL
const date = Temporal.PlainDate.from('2023-04-18'); // Creates a PlainDate object from a string.
const newDate = date.add({ days: 5 }); // Adds 5 days to the date.

press Run

28

Subtract

TEMPORAL
const date = Temporal.PlainDate.from('2023-04-18'); // Creates a PlainDate object from a string.
const previousDate = date.subtract({ weeks: 1 }); // Subtracts 1 week from the date.

press Run

29

Until

TEMPORAL
const date = Temporal.PlainDate.from('2023-04-18'); // Creates a PlainDate object from a string.
const duration = date.until(Temporal.PlainDate.from('2023-04-25')); // Duration until April 25, 2023.

press Run

30

PART 5 · LIVE

Examples

Checking your browser for Temporal…

31

Daylight saving time

1:00 AM in New York on 12 March 2023, just before the clocks jump forward. Add one day.

DATE
// Adding a day across the DST boundary with Date
const initialDate = new Date('2023-03-12T01:00:00-05:00'); // 1 AM in New York
const resultDate = new Date(initialDate.getTime() + 24 * 60 * 60 * 1000); // "one day later"
resultDate.toLocaleString('en-US', { timeZone: 'America/New_York' });

press Run

TEMPORAL
// Adding days with Temporal, considering DST
const initialDate = Temporal.ZonedDateTime.from('2023-03-12T01:00[America/New_York]');
const resultDate = initialDate.add({ days: 1 });
resultDate.toString();

press Run

Updated since the talk. The slide added a day with setDate(), which keeps wall-clock time; the bug people actually ship is adding 24 hours. The Temporal side now uses ZonedDateTime: a PlainDateTime has no time zone, so it never sees DST.

32

Time until New Year

Both run on a one-second tick in your own time zone. Count the lines of arithmetic.

DATE
function timeUntilNewYear() {
  const now = new Date();
  const year = now.getFullYear();
  const newYear = new Date(year + 1, 0, 1); // January 1st of next year
  const diff = newYear - now;

  const days = Math.floor(diff / (1000 * 60 * 60 * 24));
  const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
  const seconds = Math.floor((diff % (1000 * 60)) / 1000);

  return { days, hours, minutes, seconds };
}

press Run

TEMPORAL
function timeUntilNewYear() {
  const now = Temporal.Now.zonedDateTimeISO();
  const newYear = Temporal.PlainDate
    .from({ year: now.year + 1, month: 1, day: 1 })
    .toZonedDateTime(now.timeZoneId);
  const left = now.until(newYear, { largestUnit: 'days', smallestUnit: 'seconds' });
  return `${left.days} days, ${left.hours} hours, ${left.minutes} minutes, ${left.seconds} seconds`;
}

press Run

Updated since the talk. PlainDate.at() was replaced by toPlainDateTime() and toZonedDateTime(), and a Duration built from seconds isn't balanced into hours, so the slide's version now throws. until() with largestUnit does the balancing for you.

33

Calculating age

Try your own birthday.

DATE
function calculateAge(birthdate, currentDate) {
  const birth = new Date(birthdate);
  const current = new Date(currentDate);
  let age = current.getFullYear() - birth.getFullYear();
  const m = current.getMonth() - birth.getMonth();
  if (m < 0 || (m === 0 && current.getDate() < birth.getDate())) {
    age--;
  }
  return age;
}

press Run

TEMPORAL
function calculateFullAge(birthdate, currentDate) {
  const birth = Temporal.PlainDate.from(birthdate);
  const current = Temporal.PlainDate.from(currentDate);
  const age = current.since(birth, { largestUnit: 'years' });
  return `${age.years} years, ${age.months} months, and ${age.days} days`;
}

press Run

Watch out: new Date('1990-04-15') is read as midnight UTC. West of Greenwich, getDate() then returns the 14th, so the Date version can be a day out around a birthday.

34

Time zone conversion

Noon in New York on 18 April 2023. What time is it in Los Angeles?

DATE
// Convert a date-time from New York to Los Angeles time using the Date object
const dateInNewYork = new Date('2023-04-18T12:00:00Z');
const offsetNewYork = -240; // Offset for New York in minutes (EDT)
const offsetLosAngeles = -420; // Offset for Los Angeles in minutes (PDT)

const dateInLosAngeles = new Date(dateInNewYork.getTime() - (offsetLosAngeles - offsetNewYork) * 60 * 1000);
dateInLosAngeles.toISOString(); // Output is approximate and error-prone

press Run

TEMPORAL
// Using Temporal to handle time zone conversion accurately
const dateInNewYork = Temporal.ZonedDateTime.from('2023-04-18T12:00[America/New_York]');
const dateInLosAngeles = dateInNewYork.withTimeZone('America/Los_Angeles');
const dateWhereYouAre = dateInNewYork.withTimeZone(Temporal.Now.timeZoneId());

press Run

Updated since the talk. ZonedDateTime.from() no longer takes a { timeZone, plainDateTime } object; pass a string with the zone in brackets, or build it with PlainDateTime.toZonedDateTime().

35

Not ready for production!

UPDATE, OCTOBER 2026

That was true on the night. Since then Temporal reached Stage 4 (2025) and ships in Firefox 139 (May 2025) and Chrome 144 (January 2026). Safari still lacks the full API, so this page loads a small polyfill there.

36

Questions?

37

Get in touch