DEV Community

Cover image for JavaScript's Date Is Broken. Temporal Finally Fixes It
Muiduzzaman Lipu for Refactorly

Posted on

JavaScript's Date Is Broken. Temporal Finally Fixes It

Ask JavaScript for February 31st:

new Date(2026, 1, 31).toDateString();   // 'Tue Mar 03 2026'
Enter fullscreen mode Exit fullscreen mode

No error, just March 3rd. JavaScript's Date has been with us since 1995, and it shows. Below are four classic Date bugs and how the Temporal API fixes each one. Temporal now ships in Chrome/Edge 144, Firefox 139 and Node.js 26.

Every example was run on Node.js 26.13 with TZ=America/Chicago. The polyfill example was run on Node 22.

Video version:

Bug 1: months start at 0, and invalid dates roll over

new Date(2026, 0, 15).toDateString();   // 'Thu Jan 15 2026'  (month 0 = January)
new Date(2026, 1, 31).toDateString();   // 'Tue Mar 03 2026'  ("Feb 31" → Mar 3)
new Date('banana');                     // Invalid Date (an object, not an error)
Enter fullscreen mode Exit fullscreen mode

Month 1 is February, and out-of-range days silently overflow into the next month. Even garbage input doesn't throw; you get an Invalid Date object that travels through your code until something prints NaN.

Bug 2: Date objects mutate

const start = new Date(2026, 0, 31);   // Jan 31
const end = start;                     // not a copy!
end.setMonth(end.getMonth() + 1);      // "one month later"

start.toDateString();  // 'Tue Mar 03 2026'  start changed too
end.toDateString();    // 'Tue Mar 03 2026'  and it's not Feb 28
Enter fullscreen mode Exit fullscreen mode

Two bugs in three lines. end is the same object as start, so setMonth changes both. And "one month after Jan 31" overflows to March 3.

Bug 3: the date string that loses a day

$ TZ=America/Chicago node
> new Date('2026-03-08').getDate()
7
> new Date('2026-03-08').toString()
'Sat Mar 07 2026 18:00:00 GMT-0600 (Central Standard Time)'
> new Date('2026-03-08T00:00').getDate()
8
Enter fullscreen mode Exit fullscreen mode

A date-only ISO string is parsed as midnight UTC. In Chicago (UTC−6), that's 6 PM the previous day. Add a time (T00:00) and the same date is parsed as local time instead. This is behind countless "my date picker shows the wrong day" bugs anywhere west of UTC.

The fix: Temporal.PlainDate

A PlainDate is just a calendar date: no time, no time zone, so there's no midnight-UTC trap.

const jan31 = Temporal.PlainDate.from({ year: 2026, month: 1, day: 31 });
jan31.month;                          // 1 (January is 1)

jan31.add({ months: 1 }).toString();  // '2026-02-28'
jan31.toString();                     // '2026-01-31' (unchanged: immutable)

Temporal.PlainDate.from({ year: 2026, month: 2, day: 31 }, { overflow: 'reject' });
// RangeError: Temporal error: day value is not in a valid range.
Enter fullscreen mode Exit fullscreen mode
  • Months are 1-based.
  • Adding a month to Jan 31 constrains to the last valid day: Feb 28.
  • Every operation returns a new object.
  • overflow: 'reject' turns impossible dates into errors. The default, 'constrain', clamps them: { month: 2, day: 31 } becomes 2026-02-28.

Durations without millisecond math

const start = Temporal.PlainDate.from('2026-03-01');
const xmas  = Temporal.PlainDate.from('2026-12-25');

start.until(xmas).days;                                  // 299
start.until(xmas, { largestUnit: 'months' }).toString(); // 'P9M24D'
Temporal.PlainDate.compare(start, xmas);                 // -1
Enter fullscreen mode Exit fullscreen mode

No more (b - a) / 86400000. compare plugs straight into array.sort(Temporal.PlainDate.compare).

Bug 4: daylight saving time

On March 8, 2026, clocks in the US jump forward one hour. "Tomorrow at the same time" and "24 hours from now" are different things, and Date only does milliseconds:

const z = Temporal.ZonedDateTime.from('2026-03-07T12:00[America/Chicago]');

z.add({ days: 1 }).toString();
// '2026-03-08T12:00:00-05:00[America/Chicago]'   same wall-clock time

z.add({ hours: 24 }).toString();
// '2026-03-08T13:00:00-05:00[America/Chicago]'   exact elapsed time
Enter fullscreen mode Exit fullscreen mode

Time-zone conversion is a single call:

const meet = Temporal.ZonedDateTime.from('2026-11-23T18:00[America/Chicago]');
meet.withTimeZone('Asia/Dhaka').toString();
// '2026-11-24T06:00:00+06:00[Asia/Dhaka]'
Enter fullscreen mode Exit fullscreen mode

Can I use it today?

Runtime Temporal
Chrome / Edge 144+
Firefox 139+
Node.js 26
Safari not fully shipped yet

For websites, use the polyfill until Safari ships it:

// npm i @js-temporal/polyfill
import { Temporal } from '@js-temporal/polyfill';

Temporal.PlainDate.from('2026-01-31').add({ months: 1 }).toString(); // '2026-02-28'
Enter fullscreen mode Exit fullscreen mode

I ran that on Node 22 (which has no built-in Temporal) and got the same result.

Cheat sheet

Date Temporal
Months start at 0 Months start at 1
new Date(2026, 1, 31) → Mar 3 overflow: 'reject' throws
Mutable Immutable
'2026-03-08' parsed as UTC PlainDate has no time zone
(b - a) / 86400000 a.until(b)
Milliseconds only ZonedDateTime understands DST

What's the worst Date bug you've shipped? 👇


I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.

Top comments (0)