JavaScript traps · Chapter 9 of 16
Dates and time zones
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Time
- 3 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.
Why you need this
Most of the day, the UTC date and India's date are the same, so the report is right from 5:30 am to midnight. On a laptop set to India time, getHours gives India's hour, so the badge works where it was written and tested. Nothing throws: the code gives a real date and a real hour, just for the wrong place.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.js: put a midnight order in the wrong day
Write reproduce() in repro.js. Make the Date of an order placed at 12:30 am IST on 24 October 2026 (that instant is 7:00 pm UTC on the 23rd, 2026-10-23T19:00:00Z), call businessDay from sales.js with it, and resolve to what businessDay gives.
The check runs your reproduce() twice, on a machine in UTC like the servers: beside the original sales.js it must resolve to '2026-10-23' (the order lands in the day before), and beside a fixed sales.js it must resolve to '2026-10-24'. So it must really call businessDay with that instant. No real clock: make the instant from a string with its zone in it. reproduce() must finish within 2 seconds.
- Task 2
sales.js: India's day and India's hours, on any machine
Fix businessDay(date) and isOpen(date) in sales.js so they give India time, whatever zone the machine runs in. businessDay gives the date in IST as 'YYYY-MM-DD': an order at 12:30 am IST on 24 October 2026 belongs to '2026-10-24', one at 11:59 pm IST on the 23rd to '2026-10-23'. isOpen gives true from 6:00 am IST up to, but not including, 11:00 pm IST, and false at other times.
The check tries instants around midnight, 5:30 am, 6 am and 11 pm IST and across a new year, on a machine whose zone it sets to UTC, Asia/Kolkata and America/New_York in turn: every zone must give the same answers. Neither function may change the Date it is given. Keep stampOrder and its log line as they are. A fix that adds 5.5 hours and then reads the machine's own date and hour is right in one zone only.
- Task 3
sales.test.js: a test that would have caught it
Write a vitest test file, sales.test.js, that would have caught this bug before it shipped. The check runs your file three times, on a machine in UTC: beside the original sales.js, where at least one test must fail; beside the fixed sales.js, where every test must pass; and beside a wrong fix that adds 5.5 hours and then reads the machine's own date and hour, where at least one test must fail.
That wrong fix gives the right answers in UTC, so your test must set the zone itself: run the same expectations with process.env.TZ set to 'UTC' and to 'Asia/Kolkata', and put it back afterwards. Test businessDay just after midnight IST and in the evening, and isOpen at 6 am and 11 pm IST. Each test must finish within 3 seconds.
What you will learn
- repro.js: put a midnight order in the wrong day
- sales.js: India's day and India's hours, on any machine
- sales.test.js: a test that would have caught it
More in Time
Build "Dates and time zones" yourself
Tapri runs on JavaScript, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.