Local time and UTC Time
Hey ,
I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.
Please upgrade (use this link) access this content.
I'm super eager to help you learn more!
Local time and UTC Time
It’s harder to talk about time in JavaScript because we live in a global world. We have to account for timezone differences. Your 8AM isn’t the same as my 8AM.
The Date
methods you learned in the previous lessons all give you a value according to the local time.
Local Time
Local time refers to the timezone where JavaScript gets executed.
Let’s say we have a piece of code that sets time to 8:30am on 25th December, 2016. (We’ll talk about setting date/time in the next lesson. Follow along for now).
const date = new Date('2016-12-25T08:30')
What would the value of date
be?
- For me, its 25th Dec, 2016 at 8:30am at GMT+8 (Singapore time)
- For you, its 25th Dec, 2016 at 8:30am, but it’ll be a different timezone
GMT
GMT is an abbreviation for Greenwich Mean Time. It measures the mean solar time at the Royal Observatory in Greenwich, London.
This is the time we normally state our timezones in. For example:
- Singapore is
GMT +8
- San Francisco is
GMT -7
These +8
and -7
values aren’t called timezone differences. They’re actually called offsets.
Offsets !== timezones
This is one common misconception most people have.
Offsets aren’t the same as timezones because timezones can be affected by factors like:
- Daylight savings time
- Decisions made by local governments
This Stack overflow post goes dives into more detail why offsets aren’t the same as timezones.
This means getting timezones 100% right is hard. If you want to be 100% accurate about timezones, you’ll have to use the IANA timezone database to track offsets in every city. I highly recommend using a library like Moment Timezone if you ever need to work with timezones in JavaScript.
UTC
UTC is an abbreviation for Coordinated Universal Time (or simply, universal time). It is the time standard used to regulate the world’s clock.
For simplicity, we can treat GMT and UTC as the same thing (even though they’re not).
Notes:
- It’s UTC and not CUT 😉. Why? This article gives you some insight if you’re interested.
- GMT and UTC actually mean different things. UTC is slightly more accurate than GMT. This article can explain why.
UTC Date methods
JavaScript contains UTC versions of the date methods. Here’s a list. Notice how everything begins with getUTC
😁.
getUTCFullYear
: Gets 4-digit year according to universal time.
getUTCMonth
: Gets month of year (0-11) according to universal time.
getUTCDate
: Gets day of the month (1-31) according to universal time.
getUTCDay
: Gets day of week (0-6) according to universal time.
getUTCHours
: Gets hours (0-23) according to universal time.
getUTCMinutes
: Gets minutes (0-59) according to universal time.
getUTCSeconds
: Gets seconds (0-59) according to universal time.
getUTCMilliseconds
: Gets milliseconds (0-999) according to universal time.