The data toolbox

INPUT · Slides

Working with dates and times

01 / 05

Dates are tricky with plain numbers

Questions like "What is today's date?", "Which date came first?", and "How many days remain?" involve leap years and varying month lengths (28 to 31 days).

JavaScript provides the standard Date class to handle these complexities reliably.

02 / 05

Creating a specific date

Pass an international standard string (ISO 8601) like new Date("YYYY-MM-DDTHH:mm:ssZ").

The trailing Z stands for UTC (Coordinated Universal Time).

const date = new Date("2026-05-15T00:00:00Z");console.log(date.toISOString());

Result

2026-05-15T00:00:00.000Z

03 / 05

Extracting year, month, and day

Date instances provide getter methods:

  • .getUTCFullYear(): Year as a 4-digit number (e.g. 2026)
  • .getUTCMonth(): Month (0 to 11). January is 0, so add + 1 when displaying to humans!
  • .getUTCDate(): Day of the month (1 to 31)
const d = new Date("2026-05-15T00:00:00Z");console.log(d.getUTCFullYear());console.log(d.getUTCMonth() + 1);console.log(d.getUTCDate());

Result

2026
5
15

04 / 05

Comparing dates

Two dates can be compared with < or > just like numbers to see which is earlier or later.

Also, calling .getTime() returns elapsed milliseconds since Jan 1, 1970, which allows exact time difference calculations.

const start = new Date("2026-01-01T00:00:00Z");const end = new Date("2026-01-02T00:00:00Z");console.log(start < end);const diffHours = (end.getTime() - start.getTime()) / (1000 * 60 * 60);console.log(diffHours);

Result

true
24

05 / 05

Check the key points

Let us review the concepts:

  • Create date instances with new Date("...")
  • Get year with .getUTCFullYear(), day with .getUTCDate()
  • .getUTCMonth() starts at 0 (January is 0)
  • Compare dates using < and >

Let us practice working with dates!