JavaScript Date: UTC Fields versus Local Fields
Quick answer
Date methods can interpret a single instant using UTC or the host’s local time zone. The timestamp does not change just because its calendar fields are displayed differently.
How it works
getUTCFullYear, getUTCMonth, getUTCDate and getUTCHours read UTC fields; methods without UTC use local time. Month indices start at zero; getDate is the day of month and getDay is the weekday (0 means Sunday). Date.UTC(year, monthIndex, ...) computes a timestamp from UTC fields. Numeric years 0–99 are mapped to 1900–1999 by Date.UTC and the multiargument constructor; use an explicit ISO string for those years.
const d=new Date(Date.UTC(2024,0,2,3)); console.log(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours());Expected output in Node.js:
2024 0 2 3January has index zero; day and hour are ordinary fields.
const d=new Date("2024-01-01T01:00:00+02:00"); console.log(d.toISOString(),d.getUTCDate());Expected output in Node.js:
2023-12-31T23:00:00.000Z 31The offset converts the stated local clock time to the preceding UTC date.
Common mistakes
A local getter can report a different day from a UTC getter near midnight. Do not mix them when formatting a single date.
Practice
Return the UTC year, one-based month and day from a millisecond timestamp.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Date: UTC Fields versus Local Fields — Knowledge Check
Apply the taught concept and predict the output.
What does getUTCMonth return for January?
Which getter reports the UTC weekday rather than the day of month?
What year does Date.UTC(25, 0, 1) interpret?
What is the exact console output in JavaScript Date: UTC Fields versus Local Fields example 1?
const d=new Date(Date.UTC(2024,0,2,3)); console.log(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours());What is the exact console output in JavaScript Date: UTC Fields versus Local Fields example 2?
const d=new Date("2024-01-01T01:00:00+02:00"); console.log(d.toISOString(),d.getUTCDate());