Time Zones and UTC
Handle UTC and local time correctly.
Time Zones and UTC is a free JavaScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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())getTimezoneOffset
getTimezoneOffset returns the difference between local time and UTC in minutes. A positive value means local time is behind UTC.
const d = new Date()
const offset = d.getTimezoneOffset()
console.log(typeof offset)Offset Sign
The offset sign is the reverse of what many expect: a zone ahead of UTC gives a negative offset. Convert minutes to hours by dividing by 60.
const offsetMinutes = 120
const hours = offsetMinutes / 60
console.log(hours)Building UTC Dates
Date.UTC takes the same component arguments as the constructor but interprets them as UTC, returning a timestamp you can pass to new Date.
const ts = Date.UTC(2024, 0, 1, 0, 0, 0)
const d = new Date(ts)
console.log(d.getUTCHours())UTC Setters
Setters like setUTCHours adjust a date in the UTC zone, mirroring the local setters but anchored to UTC.
const d = new Date(Date.UTC(2024, 0, 1, 0, 0, 0))
d.setUTCHours(15)
console.log(d.getUTCHours())The Z in ISO Strings
The trailing Z in an ISO string means the time is in UTC, also called Zulu time. Strings without it may be interpreted as local.
const d = new Date("2024-01-15T10:00:00Z")
console.log(d.getUTCHours())Same Moment, Different Reads
The local and UTC getters describe the same instant in different zones, so their hour values can differ by the offset.
const d = new Date(Date.UTC(2024, 0, 1, 12, 0, 0))
console.log(d.getUTCHours())Storing in UTC
A common best practice is to store all dates in UTC and convert to local only for display, which avoids ambiguity across regions.
const d = new Date(Date.UTC(2024, 0, 1, 0, 0, 0))
console.log(d.toISOString())Specifying a Time Zone
Formatting methods accept a timeZone option so you can render a moment in a chosen zone such as UTC explicitly.
const d = new Date(Date.UTC(2024, 0, 1, 0, 0, 0))
const text = d.toLocaleString("en-US", { timeZone: "UTC" })
console.log(typeof text)Avoiding Pitfalls
Mixing local and UTC methods on the same date causes subtle bugs. Pick one model per calculation and apply it consistently.
const d = new Date(Date.UTC(2024, 0, 1, 0, 0, 0))
console.log(d.getUTCFullYear())
console.log(d.getUTCMonth())Quick Check
Test your time zone knowledge.
Recap
Recap: Read and write dates in local or UTC consistently.
- UTC getters and setters work in the global zone
- getTimezoneOffset returns minutes, sign reversed
- The Z suffix marks an ISO string as UTC
- Store in UTC, display in local
const d = new Date(Date.UTC(2024, 0, 1, 12, 0, 0))
console.log(d.getUTCHours())Frequently asked questions
Is the “Time Zones and UTC” lesson free?
Yes — the full text of “Time Zones and UTC” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Time Zones and UTC”?
Handle UTC and local time correctly. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Time Zones and UTC” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating and Reading Dates
- Timestamps and Date Math
- Formatting Dates
- Time Zones and UTC