I'm curious to know how timezone works in application.
Suppose there's an application for adding post & comments. If one user from India add a post @ 2 PM and at the same time another user from Sydney, Australia seeing that post, it'll be 6:30 PM.
I'm just taking one country example what if this application is for across world?
So how it works?
The easiest way is to just use a UNIX timestamp. It'll store the seconds since 1.1.1970 0:00 AM UTC. Most languages have date/time manipulation libraries which can easily use a UNIX timestamp (since it's very common). Also, storing an integer is very efficient :) As for JavaScript, you don't even need any external library, because Date can handle timestamps and the conversion just fine (hooray for lightweight scripts) ;)
So, when you store a time (for example 2PM IST = UTC+5:30), it is first converted to UTC and then to the UNIX timestamp. When someone in Central Europe (for example CEST) wants to view the correct date and time in their local time zone, all you have to do is convert the timestamp to UTC and then to CEST = UTC+2. They would see 10:30AM CEST.
I'm working in a project currently that's being used in several countries with time-critical articles making up the meat of the web & mobile apps. We keep it simple and save all dates in UTC format in the database. It's then up to the client to take that date and display it in a local date + time. Because devices (browsers, smartphones) have their own date set (along with a timezone) it's very easy to localise the UTC dates/times for that device. For instance, in a web app a hugely popular date/time handling utility is Moment, which is flexible enough to accept a UTC time and return a time local to the device in whatever format is required, e.g. "Mon 18th Feb, 2017" It actually doesn't matter what timezone the dates are stored in the database as, so long as the client has the logic sufficent to be able to recognise the timezone the date was stored in and then be able to convert that time into one that is local to the client.
As already pointed out by many, use UTC/GMT format to store the time and convert that while showing it to user.
Moment Js Timezone is pretty good library for frontend to handle timezone conversions between different timezones.
For starters, as many others stated, you should store all timestamps either timezone aware (ie. store the timezone, not just the date), or “naive”, but in that case, convert it to a well-known timezone first (the best one is UTC).
How to display it to the user is a different story. There are numerous ways to do it, like converting every date on the backend side, or employ the frontend for this. One frontend example may be the awesome Moment.JS library.
what do you mean by front end? are you talking about browser level or at node backend level?
Anuj Sharma
Co-founder at Appther.com | NodeJs | AngularJs | NoSql | MySql | Ionic |Next Generation APIs Developer.
Store GMT time in db. And make each and every time calculations in GMT. And change the GMT to local time. I had successfully done stuff like that.