How to store dates and time zones in JavaScript

Elena Sharovar
2 min readMar 21, 2017

--

Unique representation of some moment in history is a UNIX timestamp. UNIX timestamp it’s number of seconds passed since 1970–01–01 00:00:00 GMT (Greenwich time).

If you’re in San Francisco, and your friends is in Tokyo, and you phone your friend in the evening, you guys will have different time and date, but UNIX timestamp at the moment of call is absolutely equal.

SF Time:    Tue Feb 21 2017 18:30:00 GMT-0800
GMT: Wed Feb 22 2017 02:30:00 GMT+0000
Tokyo time: Wed Feb 22 2017 11:30:00 GMT+0900
UNIX Timestamp: 1487730600000

JavaScript Date object stores a timestamp inside, and .toString() method shows date and time in current environment’s locale. Current environment — it’s your browser or Node.js server.

How to store dates in a database? Is it enough to just save timestamp?

Imagine you have international website like meetup.com where people schedule meetups. Some person from San Francisco scheduled a meetup at this time:

Tue Feb 21 2017 18:30:00 GMT-0800

You have sent that date’s timestamp to server and saved it as:

event_date: 1487730600000

Person from Tokyo arrives to that meetup page, and if server returns date as a timestamp, and you format that date in browser, person from Tokyo will see meetup date as:

Wed Feb 22 2017 11:30:00

Ooops! But this is not true, event is planned for Feb 21 evening. You should show date & time of meetup in SF timezone, if event is happening in SF.

So, it’s not enough to store just a timestamp on server. You should store a timestamp and timezone, in which that event will happen. Easy way to detect event creator’s timezone is to get new Date().getTimezoneOffset()

event_date: 1487730600000
event_timezone_offset: 480

So, when user from Tokyo arrives, you should show date & time which will be at moment 1487730600000 in GMT+8 timezone.

How to make that in plain JavaScript?

function getEventDate(event_date, event_timezone_offset) {let d = new Date(event_date);
let timeCorrection = d.getTimezoneOffset() - event_timezone_offset;
d.setMinutes(d.getMinutes() + timeCorrection);
return `${d.getFullYear()}-${d.getMonth()}-${d.getDate()} `
+ `${d.getHours()}:${d.getMinutes()}`
}

moment.timezone.js library makes it easier for you to work with timezones. Use moment.tz.guess() to detect event creator’s timezone and store it to a database:

event_date: 1487730600000
event_timezone: America/Los_Angeles

When visitor from Tokyo arrives to event page, retrieve data from server and format event date using this code:

moment.tz(event_date, event_timezone).format("YYYY-MM-DD HH:SS")

Summary

If you need to show event date and time in event creator’s timezone— you should store both event timestamp and event creator timezone on server.

If you need to show dates in website’s visitor timezone (like Google Calendar does for meetings)- it’s enough to store event timestamp only

Database Support

This is great article explaining how Postgres works with timezones — http://blog.untrod.com/2016/08/actually-understanding-timezones-in-postgresql.html

Long story short: you’ll have to use two columns — one for timestamp, and another one — string column — for timezone. There’s no way to store both timestamp and timezone in one column.

--

--