Skip to content

Intermediate · Modules, errors, JSON and dates · topic 29 of 61 · 30 min

Dates and times

Explanation

A JavaScript Date is a single moment, stored as milliseconds since 1 January 1970 UTC. Months count from 0 (January is 0), which causes many bugs. Store and send times in UTC (ISO strings), do date maths in milliseconds, and show times in the user's zone, such as IST (UTC+5:30), with Intl.DateTimeFormat.

What a Date really is

A Date object stores one number: the milliseconds since midnight UTC on 1 January 1970 (called the Unix epoch). UTC (Coordinated Universal Time) is the world reference time with no time zone offset. Everything else (the year, the hour you see) is calculated from that number using a time zone.

javascript
const now = new Date();
console.log(now.getTime());       // e.g. 1792400000000 (ms since 1970 UTC)
console.log(Date.now());          // the same number without creating a Date
console.log(now.toISOString());   // e.g. '2026-10-19T11:40:00.000Z' (Z means UTC)

Creating dates

javascript
new Date('2026-01-15T10:00:00Z');   // from an ISO string in UTC (safest)
new Date(Date.UTC(2026, 0, 15, 10)); // from parts in UTC
new Date(2026, 0, 15);               // from parts in the LOCAL time zone
new Date(0);                         // the epoch
Careful: Months are 0-based: 0 is January and 11 is December, but days start at 1. new Date(2026, 1, 1) is 1 February. Also, out-of-range parts roll over silently: new Date(2026, 1, 30) becomes 2 March 2026, not an error.

Reading parts

getFullYear, getMonth, getDate (day of month), getDay (day of week, 0 = Sunday), getHours… use the LOCAL time zone of the computer running the code. The getUTC… versions use UTC. On a server in Mumbai and a server in Frankfurt the local getters give different answers for the same moment.

Date arithmetic

javascript
const DAY = 24 * 60 * 60 * 1000;
const booked = new Date('2026-03-01T00:00:00Z');
const journey = new Date('2026-03-15T00:00:00Z');
console.log((journey - booked) / DAY);          // 14 (subtracting Dates gives ms)
const due = new Date(booked.getTime() + 30 * DAY);
console.log(due.toISOString().slice(0, 10));    // '2026-03-31'

Time zones and India

India Standard Time (IST) is UTC+5:30 all year, with no daylight-saving changes. Many countries do change their clocks, which is why "add 24 hours" and "the same time tomorrow" are not always the same thing abroad. The safe rule: store and send times in UTC (ISO strings ending in Z), and convert to a time zone only when showing them to a person.

javascript
const d = new Date(Date.UTC(2026, 0, 15, 6, 30)); // 06:30 UTC
const ist = new Intl.DateTimeFormat('en-IN', {
  timeZone: 'Asia/Kolkata', dateStyle: 'medium', timeStyle: 'short',
}).format(d);
console.log(ist); // '15 Jan 2026, 12:00 pm' (06:30 + 5:30)

Parsing pitfalls

new Date('2026-01-15') (date only) is read as UTC midnight, but new Date('2026-01-15T10:00') (date and time, no Z) is read as LOCAL time. Strings like '15/01/2026' are not reliably parsed at all. Parse Indian-format dates yourself by splitting the parts.

Temporal: the modern date API

Because Date has so many traps, TC39 designed a new API called Temporal, with separate types such as Temporal.PlainDate (a calendar date with no time zone) and Temporal.ZonedDateTime. It is arriving in JavaScript engines; check MDN's browser compatibility table before relying on it, and until then libraries like date-fns or Day.js are common in projects.

Tip: For "is this date before that date", compare getTime() values: a.getTime() < b.getTime(). Comparing with === compares object identity and is always false for two different Date objects.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Dates and times · 1/7

Dates and times

In this lesson you will work with dates, times and time zones.

Prefer a video by a teacher? Find free videos on “javascript date object time zones utc tutorial” on YouTube ↗

In real life: A return window on an e-commerce order

An online store allows returns for 10 days after delivery. The server stores delivery time in UTC. To decide whether the return button is shown, it adds 10 days in milliseconds and compares with the current time. The customer sees the deadline converted to IST.

javascript
const DAY = 86400000;
const deliveredAt = new Date('2026-05-01T09:15:00Z');
const lastDay = new Date(deliveredAt.getTime() + 10 * DAY);
const now = new Date('2026-05-08T12:00:00Z');
console.log(now.getTime() <= lastDay.getTime()); // true: can still return
console.log(lastDay.toISOString());              // '2026-05-11T09:15:00.000Z'

Practice (0/3 done)

Task 1 · Code · Javascript

Write daysBetween(isoA, isoB) that returns the whole number of days from date A to date B (both ISO strings in UTC). daysBetween("2026-03-01T00:00:00Z", "2026-03-15T00:00:00Z") is 14.

Task 2 · Code · Javascript

Indian forms use DD/MM/YYYY. Write parseIndianDate(text) that returns the ISO date string "YYYY-MM-DD" (e.g. "15/01/2026" → "2026-01-15"), or null when the day or month is impossible (like 31/02/2026). Build the date with Date.UTC and check that it did not roll over.

Task 3 · Code · Javascript

Write isWithinReturnWindow(deliveredIso, nowIso, days) that returns true when now is at most `days` days after delivery.

Quick quiz

Question 1

1.new Date(2026, 1, 10) is which date?

Question 2

2.What does Date.now() return?

Question 3

3.What is the UTC offset of IST?

Question 4

4.Two Date objects for the same moment: a === b is…

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

new Date(2026, 1, 10) is which date?

Answer: 10 February 2026

Months are 0-based, so 1 is February.

What does Date.now() return?

Answer: Milliseconds since 1 Jan 1970 UTC

Date.now() gives the current time as a number of milliseconds.

What is the UTC offset of IST?

Answer: +5:30

India Standard Time is UTC+5:30 all year.

Two Date objects for the same moment: a === b is…

Answer: false

=== compares object identity; compare getTime() values instead.

Why are dates stored as 'YYYY-MM-DD' text in SQLite?Related

Answer: Because the date functions understand it and it sorts correctly as text

Year first and fixed widths make text order equal to date order.

From Working with dates

Why store timestamps in UTC?Related

Answer: Each moment has one unambiguous value, and display code converts it to any zone

Mixed local times cannot be compared safely. UTC is one shared clock.

From Data types, naming and good habits

sayHi(); function sayHi() { return "hi"; } Does the call work?Related

Answer: Yes

Function declarations are fully hoisted.

From Hoisting and the temporal dead zone

What does new Intl.NumberFormat('en-IN').format(1500000) give?Related

Answer: 15,00,000

en-IN groups as lakhs: 15,00,000.

From Formatting numbers, money and text with Intl

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 1 more topic in “Modules, errors, JSON and dates” before its exam: Formatting numbers, money and text with Intl.
  2. Module 6 exam: Modules, errors, JSON and dates · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Map, Set, iterators, generators and regular expressions. 61 topics left in the course before the final exam.

← Previous: JSON: data in and out