Sub topic 4
Local Time and UTC: Two Views of the Same Instant
This chapter assumes you know that a Date holds one time value in milliseconds since the Unix epoch, and that you can create dates from components and ISO strings. It also assumes you know what UTC and a time-zone offset are.
One Value, Two Readings
A Date stores no time zone. Each getter reads the same time value in one of two ways:
- UTC getters (
getUTCHours,getUTCDate, and so on) read the instant on the UTC clock. - Local getters (
getHours,getDate, and so on) first apply the offset of the runtime's time zone, then read the result.
The runtime's zone comes from the operating system or browser. In Node.js you can override it with the TZ environment variable, which makes these examples easy to reproduce.
The Getter Pairs
| Local | UTC | Returns |
|---|---|---|
| `getFullYear()` | `getUTCFullYear()` | Four-digit year |
| `getMonth()` | `getUTCMonth()` | Month, 0 to 11 |
| `getDate()` | `getUTCDate()` | Day of the month, 1 to 31 |
| `getDay()` | `getUTCDay()` | Weekday, 0 (Sunday) to 6 |
| `getHours()` | `getUTCHours()` | Hour, 0 to 23 |
Save this as views.js and run it with TZ=America/New_York node views.js:
const d = new Date("2024-0704T02:00:00Z".replace("07", "07-"));
console.log(d.toISOString()); // "2024-07-04T02:00:00.000Z"
console.log(d.getUTCDate()); // 4
console.log(d.getUTCHours()); // 2
console.log(d.getDate()); // 3 (still July 3 in New York)
console.log(d.getHours()); // 22
console.log(d.getTimezoneOffset()); // 240The local and UTC views can disagree on the hour, the day, the weekday, and even the year near New Year.
The Offset Sign Is Inverted
getTimezoneOffset() returns UTC minus local time, in minutes. New York in summer is UTC−4, so it returns 240, a positive number. Tokyo is UTC+9, so it returns -540. The value can also change through the year when daylight saving time starts or ends, so read it from the specific date you care about.
Setters Follow the Same Split
The setters come in the same pairs. Both change the stored time value. They differ in which clock they use to interpret your arguments.
const d = new Date("2024-07-04T16:30:00Z");
const localMidnight = new Date(d.getTime());
localMidnight.setHours(0, 0, 0, 0);
console.log(localMidnight.toISOString()); // "2024-07-04T04:00:00.000Z" in New York
const utcMidnight = new Date(d.getTime());
utcMidnight.setUTCHours(0, 0, 0, 0);
console.log(utcMidnight.toISOString()); // "2024-07-04T00:00:00.000Z" everywhereTo build a date from UTC components, use Date.UTC. It returns a time value in milliseconds: new Date(Date.UTC(2024, 6, 4)).
Choosing a View
- Use UTC methods for storage, server logic, logging, and any calculation that must give the same result on every machine.
- Use local methods for values the user sees or types on their own clock, such as "today" in a calendar picker.
- For display in a specific zone other than the runtime's, neither getter family helps. A later chapter covers
Intl.DateTimeFormatwith thetimeZoneoption for that.
A common bug is mixing the two views, for example reading getUTCDate() and then calling setDate(). Pick one view for each operation and use it throughout.
Exercise
In a runtime set to Asia/Tokyo (UTC+9), what do getDate(), getUTCDate(), and getTimezoneOffset() return for new Date("2024-03-10T20:00:00Z")?
Show answer
getDate() returns 11, because 20:00 UTC is 05:00 on March 11 in Tokyo. getUTCDate() returns 10. getTimezoneOffset() returns -540, because Tokyo is ahead of UTC and the method returns UTC minus local time.
Exercise
A server must group log entries by calendar day, and the result must be the same on every machine. Write a function that returns a new Date at the start of that entry's day.
Show answer
Use the UTC setters on a copy: function startOfUTCDay(date) { const r = new Date(date.getTime()); r.setUTCHours(0, 0, 0, 0); return r; }. The local setHours would give a different result on each machine, depending on its time zone.