JavaScript lesson · 17 min

JavaScript Dates

Create dates, compare timestamps, and format stable ISO strings.

Code runs locally in your browser

What you will practice

  • Create a Date from a UTC timestamp.
  • Compare dates using milliseconds.
  • Print a stable ISO date string.

What this means

A JavaScript Date represents a moment in time. Internally, it is based on milliseconds since January 1, 1970 UTC.

Dates are tricky because humans think in calendars, time zones, and daylight saving time. Computers usually compare timestamps.

`toISOString()` is useful for stable output because it prints the date in UTC instead of depending on the user's local time zone.

If this is your first time seeing this

Dates are about moments in time, but people see those moments through time zones and calendars.

When learning, UTC and ISO strings keep examples stable and less confusing.

Mini glossary

Date
A JavaScript object representing a moment in time.
Timestamp
A numeric representation of time, usually milliseconds since 1970 UTC.
ISO string
A stable date format such as `2026-01-15T12:00:00.000Z`.

Example from everyday life

A Date is like a flight departure board. The same flight happens at one exact moment, but people in different cities may see different local clock times for it.

How it works step by step

  1. `Date.UTC(2026, 0, 15, 12, 0, 0)` creates a UTC timestamp for January 15, 2026 at 12:00.
  2. `new Date(...)` wraps that timestamp in a Date object.
  3. Subtracting two Date objects gives the difference in milliseconds.
  4. The code converts milliseconds into days by dividing by 1000, 60, 60, and 24.

Where you will use this

  • A subscription page calculates days until renewal.
  • A log viewer sorts events by timestamp.
  • A scheduling app stores UTC dates and formats them for the user's locale.

Before you run the code

Use UTC and ISO strings when you need stable examples, APIs, logs, or tests.

Use local formatting only when you are intentionally showing a date to a person in their own time zone.

Common beginner mistakes

  • Forgetting that months in `Date.UTC` are zero-based: January is 0.
  • Mixing local time and UTC without noticing.
  • Comparing formatted date strings instead of timestamps.

Run in browser

Try the example

Console output
Run the code to see console output here.
Exercise checks0 of 2 checks passed.
WaitingPrints a stable ISO start date
WaitingCalculates the day difference

Code runs locally in a temporary browser worker with a timeout. It is not sent to Lumio analytics or executed on the server.

Try changing this next

  • Change the deadline day and recalculate days left.
  • Print `start.getTime()` to see the timestamp.
  • Try `toLocaleString()` and compare it with `toISOString()`.