JavaScript Intl.DateTimeFormat: Explicit Time Zones
Quick answer
Intl.DateTimeFormat displays a time value using locale-dependent calendar and clock conventions. A time zone option controls which local calendar fields are shown.
How it works
Create a formatter with new Intl.DateTimeFormat(locale, {timeZone, ...options}). Choose calendar and numbering system explicitly when relevant. formatToParts gives labeled components such as year, month, day and hour. formatRange and formatRangeToParts can display date ranges in supporting runtimes. Formatting does not change the original instant; locale and time zone data can differ between implementations.
const d=new Date("2024-01-01T01:00:00Z"); const f=new Intl.DateTimeFormat("en-US",{timeZone:"America/New_York",day:"numeric"}); console.log(f.format(d));Expected output in Node.js:
31At that instant New York is still on December 31.
const f=new Intl.DateTimeFormat("en-US",{timeZone:"UTC",year:"numeric",month:"2-digit",day:"2-digit"}); console.log(f.formatToParts(new Date(0)).find(p=>p.type==="year").value);Expected output in Node.js:
1970The UTC epoch is in Gregorian year 1970 for the selected locale.
Common mistakes
Never infer a time zone from a locale alone. A language preference and a user’s desired time zone are independent settings.
Practice
Return the two-digit day field of a fixed instant in a specified zone using formatToParts.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Intl.DateTimeFormat: Explicit Time Zones — Knowledge Check
Apply the taught concept and predict the output.
Which formatter option chooses the display time zone?
Does changing the formatter timeZone modify a Date timestamp?
Why can localized date strings differ across runtime versions?
What is the exact console output in JavaScript Intl.DateTimeFormat: Explicit Time Zones example 1?
const d=new Date("2024-01-01T01:00:00Z"); const f=new Intl.DateTimeFormat("en-US",{timeZone:"America/New_York",day:"numeric"}); console.log(f.format(d));What is the exact console output in JavaScript Intl.DateTimeFormat: Explicit Time Zones example 2?
const f=new Intl.DateTimeFormat("en-US",{timeZone:"UTC",year:"numeric",month:"2-digit",day:"2-digit"}); console.log(f.formatToParts(new Date(0)).find(p=>p.type==="year").value);