Time Zones and moment isBetween(‘yesterday’, ‘today’)

  1. Origin of Time Zones
  2. The Law and Time Zones
  3. Browser’s Time Zone Support and Its Limitations
  4. Time Zone Data and Its Problems
  5. Front End’s Solution
  6. Goodbye moment.js

For Koreans, Summer Time is not a very familiar concept. For those who live in the effective ranges of Summer Time, there are days when a day is 25 hours or 23 hours long. We usually take 24-hour days as the norm, but why do some countries fixate on such fluctuation?

Time zone is an idea to represent different times all around the world. Browsers, in order to account for these time zones, offer multiple solutions. However, these solutions are not enough to satiate the needs for time zone management of the web services’ users.

This article serves to illustrate why we need time zones in the first place, its history, and how to properly support time zones in the front end. Time zone is a challenge that every developer will face at some point in one’s career. Let’s take this opportunity to fully understand the it.

Origin on Time Zones

Time zones are results of significant consideration of the Earth’s rotation created to artificially correct the difference in day and night around the world. If someone in the UK wants to go to work at nine in the morning and someone in Korea wants to go to work at nine in the morning, the person in Korea can just go to work nine hours earlier than the person in the UK. This is why the Korean Standard Time is also referred to as +09:00. Laws have declared the Greenwich Time (GMT) the standard, +00:00, since 1675, so the history is long.

The Law and Time Zones

If you take a close look at time zones, there is not a single place where the time zone is the same just because they have the same longitude. Even Seoul and Gangneung can have different time zones. Therefore, people have meddled with the time zones according to the country or a certain part of the country. However, time zones went through even more altercations due to history, political, or economic reasons.

For example, Netherlands used to use incredibly specific offset of +00:19:32.13. However, they later changed to using a more standardized +01:00.

Korea is thin widthwise. There is not much of a need to specify intricate time zones. Then, how about a country like China that is wide? Does China also use one time zone like Korea, or does it have multiple time zones in different provinces? The answer is “they use a single time zone.” China maintains a single time zone despite its wide area. Therefore, you can see the Sun come up at ten in the morning in some western parts of China.

On the other hand, United States, a country that is also wide, uses multiple time zones for different states. We can observe that the time zones in the western parts of the United States and time zones in the eastern parts of the United States differ by three hours in the same country.

(Source: Namu Wiki Time Zones)

The country of Samoa used to use -10:00 time zone until they decided to change the time zone to +14:00 to minimize the loss created by the time difference with Australia and New Zealand, their trading partners. Due to this incident, people who had their birthdays on December 30th in Samoa losing their birthdays in the year 2011.

Time Zones and Offset

Summer Time

If you’re someone like me who has lived in Korea for my entire life and have never been to any study abroad programs, Summer Time is not a familiar concept. It’s natural to hear things like “that place is ahead of Korea by so-and-so hours,” or “it’s night here, and it’s morning in Korea,” but it’s just more complicating to find out that there is another twist in the Winter because of the Summer Time.

Browser’s Time Zone Support and Its Limitations

Time Zone Data and Its Problems

Therefore, time zones must manage a database of change history. The most credible database right now is the IANA Time Zone Database. While Windows maintains its own database, Java’s VM uses and regularly updates the IANA database.

This exact issue is why it is difficult to implement different time zones with JavaScript. Given ECMAScript’s ambiguous specifications and the fact that browsers do not have these databases, we cannot simply change the time zone using the Date type.

Front End’s Solution

Built-In Time Zone Database

moment.js defines the offset according to the following json database.

name : 'America/Los_Angeles', // the unique identifier
abbrs : ['PDT', 'PST'], // the abbreviations
untils : [1414918800000, 1425808800000], // the timestamps in milliseconds
offsets : [420, 480] // the offsets in minutes

Source: Zone object — https://momentjs.com/timezone/docs/

moment.js and js-joda

JavaScript does not offer a way to convert time zones using the Date and the Intl.DateTimeFormat, but moment.js and js-joda can be utilized to support older browsers and implement the time zones directly.

Goodbye moment.js

ECMAScript’s Internationalization Features and Specs

The Intl.DateTimeFormat has the option to specify the name of the time zone in the IANA database. Then, the format() function returns the string (GMT-8) and this can be parsed to obtain the offset. It can be a hassle to implement it manually, and perhaps this is why we use libraries. Libraries like dayjs, luxon, date-fns, and more offer features where the Intl can be processed to obtain useful information about date and time zones, conveniently. Such libraries can take the place of moment.js in its vacancy.

Closing Words

Time zones can be a weird concept for someone who, like myself, lived only in one place. However, understanding time zones, why the time zones cannot be programmatically encoded, and why databases are used for time zones, can be useful in creating a service that is used worldwide. I hope this article has helped.


JavaScript UI Library Open Source by http://ui.toast.com