# Dates, Time & Intl

> Navigate the complexities of timezones, the Date object, and the modern Intl API for robust localization without external libraries.

Subject: JavaScript | Lesson 54 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/dates-and-time
Previous: [Regular Expressions](https://codewithanimation.in/javascript/regular-expressions.md) | Next: [Debounce & Throttle](https://codewithanimation.in/javascript/debounce-throttle.md)

## The Date Object

JavaScript's `Date` object represents a single moment in time. Under the hood, it stores time as the number of milliseconds since the 'Unix Epoch' (January 1, 1970, UTC).

```javascript
const now = new Date();
const specific = new Date('2025-12-25T10:00:00Z');
```

## The Timezone Trap

Dates in JavaScript are inherently timezone-agnostic (they just store UTC milliseconds). The confusion arises because methods like `.getHours()` or `.toString()` automatically convert that UTC time into the user's *local* timezone.

If you save an event at 10:00 AM in New York, and a user in Tokyo opens their browser, `.getHours()` will translate that same moment in time to their local evening.

## The Intl API

Historically, developers relied on massive libraries like `moment.js` to format dates. Today, modern browsers have the incredibly powerful `Intl.DateTimeFormat` built-in.

```javascript
const formatter = new Intl.DateTimeFormat('en-GB', {
  dateStyle: 'full',
  timeStyle: 'short',
  timeZone: 'Asia/Tokyo'
});
console.log(formatter.format(now));
```

With a single native object, you can translate a timestamp into any language and format it for any timezone on Earth.

## Common mistake: Date Math

Never try to add '1 month' by manually adding days (since months vary from 28 to 31 days). Always let the `Date` object handle the math:

`const d = new Date(); d.setMonth(d.getMonth() + 1);`

The native object automatically handles leap years and month boundaries perfectly.

## Tip: ISO 8601 Strings

When sending dates to a database or API, always use `.toISOString()`. It converts the date to a strict, universal string format ending in 'Z' (meaning UTC time, or 'Zulu' time). Example: `2025-12-25T10:00:00.000Z`.

## Try it: The Intl Formatter

Select a Locale and a Timezone below. Watch how the exact same JavaScript `Date` object automatically reshapes itself to fit different cultural standards and global clocks.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/dates-and-time

## Check yourself

### Question 1

What does a JavaScript `Date` object actually store under the hood?

- A. The number of milliseconds since January 1, 1970, UTC.
- B. A formatted string like 'YYYY-MM-DD'.
- C. The local time of the user's computer.

Correct answer: A

A Date object is fundamentally just a number: milliseconds since the Unix Epoch. This makes comparing dates as easy as comparing two numbers.

- Why not B: Incorrect. The string is only generated when you call a formatting method.
- Why not C: Incorrect. It stores absolute UTC time, but displays local time by default.

### Question 2

Why is `Intl.DateTimeFormat` vastly superior to manual date formatting?

- A. It natively handles complex localization, translations, and timezones without requiring huge external libraries.
- B. It is the only way to get the current year.
- C. It automatically saves the date to the database.

Correct answer: A

The `Intl` API gives you access to the browser's deeply integrated globalization data, allowing you to format dates for any culture (e.g., 'en-US' vs 'ja-JP') natively.

- Why not B: Incorrect. You can get the year via `date.getFullYear()`.
- Why not C: Incorrect. It is purely for formatting strings for display.

### Question 3

Which format is the standard, safest way to send a Date to a backend server?

- A. ISO 8601 string (via .toISOString())
- B. Local time string (via .toLocaleString())
- C. Unix Epoch Seconds

Correct answer: A

`.toISOString()` provides a universally recognized, timezone-agnostic (UTC) string that every database and backend language can parse effortlessly.

- Why not B: Incorrect. Local strings are ambiguous and strip timezone data.
- Why not C: Incorrect. JS uses milliseconds, so seconds can cause math errors.

## Remember this

- A JavaScript `Date` object stores a single moment in time (UTC milliseconds since 1970).
- Methods like `.getHours()` automatically convert the UTC time into the user's local timezone.
- Use `.toISOString()` to generate a safe, universal string for APIs and databases.
- Let the `Date` object handle date math using `.setMonth()`, `.setDate()`, etc.
- Use the native `Intl.DateTimeFormat` API for powerful, library-free localization and timezone conversion.
