H.dev

The Date Object & Temporal API

Managing Time in JavaScript

Working with dates and times is a notorious challenge in programming. Timezones, daylight saving time, and leap years make it incredibly complex. Historically, JavaScript has handled this using the built-in Date object.

The Traditional Date Object

You can create a new date object representing the current date and time using new Date().

const now = new Date();
console.log(now); // e.g., Wed Aug 06 2026 13:00:00 GMT+0530

You can extract specific pieces of information using Date methods:

const today = new Date();

console.log(today.getFullYear()); // 2026
console.log(today.getMonth());    // 7 (August - wait, Months are 0-indexed!)
console.log(today.getDate());     // 6 (Day of the month)
console.log(today.getDay());      // 4 (Day of the week, 0 is Sunday)

Warning: The traditional Date object is widely considered to have a flawed design. The fact that months are 0-indexed (January is 0, February is 1) but days are 1-indexed has caused millions of bugs worldwide.

The Future: The Temporal API

Because the Date object is so problematic, the JavaScript community has introduced the Temporal API as its modern replacement.

Instead of one confusing Date object, Temporal provides different objects for different needs:

  • Temporal.Now.plainDateISO(): For a simple calendar date (no time).
  • Temporal.Now.zonedDateTimeISO(): For a date and time tied to a specific timezone.
// Getting the exact date without worrying about confusing timezones
const myBirthday = Temporal.PlainDate.from('2026-08-06');
console.log(myBirthday.year); // 2026
console.log(myBirthday.month); // 8 (Months are 1-indexed now! Yay!)

(Note: As of writing, you may need a polyfill to use Temporal in older browsers).

💡 Himanshu's Tip:

When you are building production apps, I highly recommend checking out my blog post on how the Temporal API replaces the old Date object! Until Temporal is 100% natively supported everywhere, libraries like date-fns or dayjs are lifesavers for formatting dates cleanly.

Interview Questions

  1. Why does new Date().getMonth() return 0 for January?
  2. What is the fundamental problem with the traditional JavaScript Date object that the Temporal API solves?
  3. How do you get the current timestamp (milliseconds since 1970) using the Date object?

Design & Developed by Himanshu Dubey © 2026