FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Dates, Time & Intl

Intermediate

Date

Date represents a moment in time as milliseconds since the Unix epoch. It is useful, but it has old design choices that require care.

const now = new Date();
const timestamp = now.getTime();

console.log(now.toISOString());
console.log(timestamp);

Dates, Time & Intl

Date stores an instant, not a clean calendar model.

A Date object represents one exact instant in time. Internally, that instant is stored as a number of milliseconds since 1970-01-01T00:00:00.000Z.

Date can show that same instant in local time or UTC. This is useful, but it also creates confusion because the same object can appear different depending on which method you call.

Date is old and widely supported. Learn it well, but treat parsing, month numbers, mutation and time zones with extra attention.

new Date()

Creates a Date for the current instant.

Timestamp

getTime returns milliseconds since the Unix epoch.

UTC output

toISOString returns a stable ISO string in UTC.

Invalid Date

Failed parsing can create a Date whose time value is NaN.

Examples

Make the date-time concept visible in the code.

Use stable input and validate it

const date = new Date("2026-01-01T09:30:00Z");

if (!Number.isNaN(date.getTime())) {
  console.log(date.toISOString());
}

Parsing vague date text

const date = new Date("01/02/2026");

// This can be read differently by people and systems.

Code patterns

Reusable examples for quick reference.

Dates and time zones become much easier when every example names the concept it represents: instant, local display, UTC output, calendar date, formatted text or zoned time.

Create the current instant

Use new Date() when you need the current moment.

const now = new Date();

console.log(now.toISOString());

Create from a timestamp

A timestamp is milliseconds since the Unix epoch.

const timestamp = 1767225600000;
const date = new Date(timestamp);

console.log(date.toISOString());

Create from ISO text

ISO strings are the safest Date parsing input.

const date = new Date("2026-01-01T09:30:00Z");

console.log(date.toISOString());

Validate a Date

Check the internal time value before using it.

const date = new Date("not a date");
const isValid = !Number.isNaN(date.getTime());

console.log(isValid);

Rules that matter

Separate storage, logic and display.

Date-time bugs usually start when storage format, calculation logic and user-facing display are treated as the same thing. Keep those responsibilities separate and the code becomes much easier to trust.

Prefer ISO input

ISO date-time strings are clearer than regional text formats.

Validate parsed dates

Invalid Date objects still exist, but their time value is NaN.

Remember Date stores an instant

Display can change by time zone, but the instant is the same.

Use getTime for numeric comparison

Comparing timestamps is clearer than comparing formatted strings.

Avoid vague parsing

Do not rely on regional date strings such as 01/02/2026.

Know the old traps

Months are zero-based in Date constructors and getters.

Production thinking

Time is a data-modeling problem before it is a formatting problem.

Why does this matter?

Date bugs often appear only for some users, some time zones or some date formats. Stable input and validation prevent quiet failures.

Accessibility

Dates shown to users should be readable, complete and not rely only on visual order such as 01/02/2026.

Production note

Production code should store stable timestamps or ISO values, validate input and format display at the edge.

SEO note

Published dates, updated dates and event dates should be stable, machine-readable and consistently formatted.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.

Mini assignment

Try this now.

  • Try an invalid value and inspect the output.
  • Show date.toString() under the ISO output.
  • Compare two dates with getTime().

Practice assignment

Do this before moving to the next topic.

  1. Create a Date for the current instant.
  2. Create a Date from an ISO string.
  3. Validate a Date with getTime and Number.isNaN.

Try it yourself

Validate and display a Date

Live preview

Self-check

Before you continue, prove that you understand Date.

Intermediate

Dates are full of traps. Explain the concept first, then run the code and check whether the output matches your mental model.

  1. Can you explain what Date stores internally?
  2. Can you explain why ISO input is safer?
  3. Can you explain what Invalid Date means?
  4. Can you explain why getTime is useful?
  5. Can you explain why display can change by time zone?