Intermediate · Modules, errors, JSON and dates · topic 29 of 61 · 30 min
Dates and times
Explanation
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.
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
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 epochReading 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
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.
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.
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
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.
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)
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.
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.
Write isWithinReturnWindow(deliveredIso, nowIso, days) that returns true when now is at most `days` days after delivery.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Formatting numbers, money and text with IntlModules, errors, JSON and dates · same module
- Error handling: try, catch, finally and throwModules, errors, JSON and dates · same module
- Working with datesSQL and Databases · Functions, CASE and NULL
- Data types, naming and good habitsSQL and Databases · Schema design and normalisation
- Days, months and datesSpoken English: Zero to Fluent · Numbers, time and money
- Files, time and structured logsGo Programming (backend and cloud) · Standard library essentials
Questions people ask about this
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.
sayHi(); function sayHi() { return "hi"; } Does the call work?Related
What does new Intl.NumberFormat('en-IN').format(1500000) give?Related
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 more topic in “Modules, errors, JSON and dates” before its exam: Formatting numbers, money and text with Intl.
- Module 6 exam: Modules, errors, JSON and dates · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Map, Set, iterators, generators and regular expressions. 61 topics left in the course before the final exam.