Time Zones and UTC
Handle UTC and local time correctly.
Local Versus UTC
A date stores one absolute moment, but it can be read in local time or in UTC, the global reference time. The getters come in both flavors.
const d = new Date(Date.UTC(2024, 0, 15, 12, 0, 0))
console.log(d.getUTCHours())UTC Getters
Methods prefixed with UTC, like getUTCFullYear and getUTCHours, read date components in the UTC zone regardless of the machine.
const d = new Date(Date.UTC(2024, 5, 15, 8, 30, 0))
console.log(d.getUTCFullYear())
console.log(d.getUTCHours())
console.log(d.getUTCMinutes())All lessons in this course
- Creating and Reading Dates
- Timestamps and Date Math
- Formatting Dates
- Time Zones and UTC