Sub topic 4
Local Time and UTC: Two Views of the Same Instant
This chapter assumes you know JavaScript basics and can run code in Node.js or a browser console. It also assumes you have read the earlier chapters. From those you should know that a Date holds one number, the milliseconds since the Unix epoch, and how to create a Date from components or an ISO 8601 string. You should also know roughly what UTC and time zones are.
The main idea of this chapter is that a Date has no time zone. It stores one instant. Every method that returns a year, month, day, or hour reads that instant through one of two lenses:
- UTC: Coordinated Universal Time, with no offset applied.
- Local time: the time zone of the runtime the code is running in, using that zone's offset rules for that specific instant.
Most getters and setters come in pairs, one for each lens. Picking the wrong one causes bugs that pass on your machine and fail on a user's.
How the two views are computed
When you call getHours(), the engine looks up the runtime's time zone (for example Europe/Berlin). It finds the UTC offset that zone had at that instant, adds the offset to the time value, and splits the result into calendar fields. getUTCHours() skips the offset step. The stored number never changes.
Reading both views
The output below depends on your runtime's zone. In Node.js you can choose the zone for one run with the TZ environment variable, for example TZ=Asia/Tokyo node views.js. This lets you check behaviour in several zones without changing your system settings.
// views.js
const d = new Date("2024-07-04T23:30:00Z"); // an explicit UTC instant
console.log("UTC: ", d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(),
d.getUTCDay(), d.getUTCHours(), d.getUTCMinutes());
console.log("Local:", d.getFullYear(), d.getMonth(), d.getDate(),
d.getDay(), d.getHours(), d.getMinutes());
console.log("Offset (minutes):", d.getTimezoneOffset());
console.log("Time value:", d.getTime());Here are the results in three zones:
| TZ | UTC fields | Local fields | getTimezoneOffset() |
|---|---|---|---|
| UTC | 2024 6 4 4 23 30 | 2024 6 4 4 23 30 | 0 |
| America/New_York | 2024 6 4 4 23 30 | 2024 6 4 4 19 30 | 240 |
| Asia/Tokyo | 2024 6 4 4 23 30 | 2024 6 5 5 8 30 | -540 |
In Tokyo, the local view is already Friday, July 5 (getDay() returns 5). The UTC view is still Thursday, July 4. Both are correct. They describe the same instant. getTime() prints 1720135800000 in all three runs.
The offset also belongs to the instant, not to the zone as a whole. In New York, a January date returns 300 and a July date returns 240. Never read the offset from new Date() once and apply it to other dates.
The paired methods
| Field | Local getter / setter | UTC getter / setter |
|---|---|---|
| Year | `getFullYear` / `setFullYear` | `getUTCFullYear` / `setUTCFullYear` |
| Month (0–11) | `getMonth` / `setMonth` | `getUTCMonth` / `setUTCMonth` |
| Day of month (1–31) | `getDate` / `setDate` | `getUTCDate` / `setUTCDate` |
| Weekday (0 = Sunday) | `getDay` (no setter) | `getUTCDay` (no setter) |
| Hours | `getHours` / `setHours` | `getUTCHours` / `setUTCHours` |
| Minutes | `getMinutes` / `setMinutes` | `getUTCMinutes` / `setUTCMinutes` |
| Seconds | `getSeconds` / `setSeconds` | `getUTCSeconds` / `setUTCSeconds` |
| Milliseconds | `getMilliseconds` / `setMilliseconds` | `getUTCMilliseconds` / `setUTCMilliseconds` |
Two methods have no pair because they do not depend on a zone: getTime/setTime, which read and write the raw time value, and getTimezoneOffset. The same split applies to construction. new Date(2024, 6, 4) reads its components as local time. Date.UTC(2024, 6, 4) reads them as UTC and returns a number, which you pass to new Date(...).
Setters: same call, different instants
A setter changes fields in its own view and then converts back to a time value. "Start of the day" therefore means different instants depending on which setter you use:
const d = new Date("2024-07-04T23:30:00Z");
const localStart = new Date(d.getTime());
localStart.setHours(0, 0, 0, 0); // midnight in the runtime's zone
const utcStart = new Date(d.getTime());
utcStart.setUTCHours(0, 0, 0, 0); // midnight UTC
console.log(utcStart.toISOString()); // "2024-07-04T00:00:00.000Z" everywhere
console.log(localStart.toISOString()); // New York: "2024-07-04T04:00:00.000Z"
// Tokyo: "2024-07-04T15:00:00.000Z"In Tokyo, the local start of the day is July 5 00:00 local time, which is 15:00 UTC on July 4. If a server running in UTC groups orders "by day" and a browser in Tokyo does the same, they will put the same order on different days. Neither result is wrong, but the two must agree on one lens.
Bugs that come from mixing the views
Reading one view and writing the other. This code reads a local field and writes a UTC field:
const d = new Date("2024-07-04T23:30:00Z");
d.setUTCDate(d.getDate() + 1); // local date + 1, written into UTC
console.log(d.toISOString()); // Tokyo: "2024-07-06T23:30:00.000Z" (two days later)In UTC and New York, this prints July 5, which looks correct. In Tokyo the local date is already 5, so the result jumps to July 6. Always use matching pairs: getUTCDate with setUTCDate, or getDate with setDate.
Confusing getDay with getDate. getDay returns the weekday (0–6). getDate returns the day of the month. Because a weekday of 4 looks like a plausible date, this mistake can go unnoticed.
Tests that only run in UTC. CI machines and containers often default to UTC, where both views give the same values. Mixing bugs stay hidden until a user in another zone hits them. Run date tests with at least one zone that has a positive offset and one with a negative offset, for example TZ=Asia/Tokyo and TZ=America/Los_Angeles.
Trusting the string methods to agree. toString() and toDateString() show local time. toISOString() and toUTCString() show UTC:
const d = new Date("2024-07-04T23:30:00Z");
console.log(d.toISOString()); // "2024-07-04T23:30:00.000Z"
console.log(d.toUTCString()); // "Thu, 04 Jul 2024 23:30:00 GMT"
console.log(d.toString()); // Tokyo: "Fri Jul 05 2024 08:30:00 GMT+0900 (Japan Standard Time)"The zone name in parentheses depends on the engine. Do not parse it.
Building a local ISO string
Sometimes you need the local wall-clock time together with its offset, such as 2024-07-05T08:30:00+09:00. This string records both the local reading and the exact instant. toISOString always uses UTC, so you build it from local getters and a negated getTimezoneOffset:
function pad(n, width = 2) {
return String(n).padStart(width, "0");
}
function toLocalISO(date) {
const offsetMin = -date.getTimezoneOffset(); // flip to the usual sign
const sign = offsetMin >= 0 ? "+" : "-";
const abs = Math.abs(offsetMin);
return (
`${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}` +
`T${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}` +
`${sign}${pad(Math.floor(abs / 60))}:${pad(abs % 60)}`
);
}
const d = new Date("2024-07-04T23:30:00Z");
console.log(toLocalISO(d)); // Tokyo: "2024-07-05T08:30:00+09:00"
// New York: "2024-07-04T19:30:00-04:00"
console.log(new Date(toLocalISO(d)).getTime() === d.getTime()); // trueThe last line shows that no information is lost. The string parses back to the same instant. The function handles zones with offsets that are not whole hours, such as Asia/Kolkata (+05:30), because it splits the offset into hours and minutes.
Choosing a view
| Task | Use |
|---|---|
| Storing, sending, or comparing instants | `getTime()` or `toISOString()`; zones don't matter |
| Server-side grouping, reports, cron-style schedules | UTC getters and setters |
| Calendar logic for the person using this device ("today", "this month") | Local getters and setters |
| Showing a time in a zone other than the runtime's | Neither; use `Intl.DateTimeFormat` with `timeZone` (covered in later chapters) |
The last row matters. A Date can only read two zones: UTC and the runtime's own zone. If you need New York time on a server in Frankfurt, no getter will give it to you. Do not shift the time value by an offset to fake it. That creates a different instant that only looks right, and it breaks across daylight saving changes. A later chapter covers these changes in detail.
Exercise
A server runs with TZ=UTC and a browser runs in America/Los_Angeles (UTC−7 in summer). Both run new Date("2024-08-01T03:00:00Z").getDate(). What does each print, and which method would make them agree?
Show answer
The server prints 1. The browser prints 31, because 03:00 UTC on August 1 is 20:00 on July 31 in Los Angeles. Calling getUTCDate() on both prints 1 on both. Calling getDate() on both agrees only when both run in the same zone.
Exercise
Why is the following wrong, and how do you fix it? const d = new Date(); d.setUTCHours(d.getHours() + 2);
Show answer
It reads the local hour and writes it as a UTC hour, so the result is off by the local offset (zero only when the runtime is in UTC). Use a matching pair: d.setHours(d.getHours() + 2) or d.setUTCHours(d.getUTCHours() + 2). To add an exact duration, d.setTime(d.getTime() + 2 * 60 * 60 * 1000) is simplest.
Exercise
In Asia/Kolkata (UTC+05:30), what does getTimezoneOffset() return, and what offset string does toLocalISO produce?
Show answer
It returns -330. toLocalISO negates it to 330 and splits it into 5 hours and 30 minutes, producing "+05:30".