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.
Welcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson