Time and Time Zone Headaches! (JS and more) | by Aideen Nasirishargh | MediumSitemapOpen in appSign up<br>Sign in
Medium Logo
Get app<br>Write
Search
Sign up<br>Sign in
Time and Time Zone Headaches! (JS and more)
Aideen Nasirishargh
12 min read·<br>Nov 12, 2021
Listen
Share
Background<br>Storing and passing “time” in programming and software developement world , full of numbers, sounds simple. But, in fact, it is very tricky. The problem is “current time in San Francisco” is different from “current time in New York”.<br>Yes, we are talking about “time zones.”<br>Press enter or click to view image in full size
Photo by Jon Tyson on UnsplashOne might think a simple solution is storing the time as a human readable version. e.g. "2021/11/12 11:23:56am in San Francisco". The problem is one might refer to the same point of time as "2021/11/12 2:23:56pm in New York" And comparing them, using == operators would be painful!<br>That’s why the standard solution is UNIX Epoch Time.<br>[UNIX] Timestamp<br>UNIX Epoch Time, or UNIX timestamp, or shortly Timestamp is simply the number of seconds passed since 00:00:00 UTC on 1 January 1970 . So let’s say someone was in London on the midnight of January 1st of 1970 and started counting from zero, going up by one number every second, and yelling that number to the entire world through a live broadcast! Let’s assume that person is still alive — Timestamp is what she is yelling now. :)<br>https://www.unixtimestamp.com/ shows current timestamp live, and provides a cool convertor. Right now it is: 1636742361.<br>UNIX timestamp doesn’t have anything to do with where you are, what language do you speak, or whether or not you observer Daylight Saving there or not. That’s why we referred to it as a live broadcast on the Internet!<br>In JavaScript, you can get it for current time via +new Date().<br>Note that:<br>JavaScript returns timestamps as milliseconds! Some other languages/libraries do seconds. So you need Math.round(new Date()/1000)
Press enter or click to view image in full size
Photo by Markus Spiske on UnsplashExchanging a “Time” value between Client and Server<br>So let’s say your client code is dealing with time. e.g. showing when a message/notification is sent or received.<br>Two rule of thumbs when dealing with time between client/server:<br>Never trust a date/time in the Client machine. Users can play with it or they clock might not be update. (Think about malicious users)<br>If you want to show a Time/Date to the user in the UI, do the Timestamp to String conversion in the client side . You can get native formatting for free.<br>For instance, the following line displays what was the time in “Kathmandu” (its time zone is UTC+5:45!) at 10:21:11am of November 11th of 2021, in San Francisco (PST), in Persian locale (locale = language + other regional configs, e.g. Iranian Calendar here). :)<br>new Date(“2021/11/12 10:21:11 AM PST”).toLocaleString(“fa”, { timezone: “Asia/Kathmandu” });<br>// Output: '۱۴۰۰/۸/۲۲، ۰:۰۶:۱۱'(If you can’t read Persian in the output above, the digits are: “1400/8/22 0:06:11”. Yes, that’s even a different calendar!)<br>Why should we ever store/pass around Time Zone between Client and Server?<br>Generally, you shouldn’t!<br>Ideally, the Server should deal with time as the “moment”s (Timestamp) all the time in logs, databases, etc., while the Client should handle the time zone conversion.<br>Press enter or click to view image in full size
Photo by Sigmund on UnsplashUnless, however, when you really need to know “what time of the day, in 24 hours, would ‘this time’ be, in the user’s location ” and the business logic of your code in the Server Side , depends on it.<br>Examples would be:<br>You need to send the user an email/notification with “Good Morning” or “Good Night” header from the Server.<br>You need to measure the user’s daily performance/limits, and you want it to reset that at “midnight in the user’s location” , not the server’s.<br>Where is zero for Time Zone? GMT vs UTC…<br>You probably have heard of both GMT (Greenwich Mean Time) and UTC (Coordinated Universal Time — should have been CUT ;D) when it comes to time/time zones.<br>Press enter or click to view image in full size
Photo by Rumman Amin on UnsplashIs GMT the Daylight Saving version of UTC? Does GMT change by Daylight Saving?<br>Nope! Neither GMT nor UTC have Daylight Saving.<br>Is GMT = Current Time at the Royal Observatory in Greenwich, London, UK?<br>Nope! UK has their own Daylight Saving (BST) and during summer London becomes GMT+1:00!<br>Do they represent different values?<br>Nope! They are mostly the same value, but representing kinda different things. e.g. Number of tires of a typical car including the spare tire, vs number of fingers of a typical hand.<br>While the first search result for “GMT vs UTC” is https://www.timeanddate.com/time/gmt-utc-time.html which says “GMT is a Time Zone, UTC is a time”, some people argue that it’s not correct, including the Wikipedia’s definitions (GMT vs UTC.)<br>Based on the best answer I found — They are basically the same in terms of...