Automatic timezone conversion in JavaScript

By David Mytton,
CEO & Founder of Server Density.

Published on the 26th October, 2016.

Editor’s note: This is an updated version of an article originally published here on 21/01/2010.

It’s been awhile since JavaScript charts and graphs became the go-to industry norm for data visualization. In fact we decided to build our own graphing engine for Server Density several years ago. That’s because we needed some functionality that was not possible with the Flash charts we used earlier. Plus, it allowed us to customize the experience to better fit our own design.

Since then we’ve been revamping the entire engine. Our latest charts take advantage of various modern JS features such as toggling line series, pinning extended info and more.

Automatic timezone conversion in JavaScript 1

Switching to a new graphing engine was no painless journey of course. JS comes with its own challenges, one of which is automatic timezone conversion.

Timezones are a pain

Timezone conversion is one of the issues you should always expect to deal with when building JS applications targeted at clients in varying timezones. Here is what we had to deal with.

Our new engine supports user preferences with timezones. We do all the timezone calculations server-side and pass JSON data to the Javascript graphs, with the timestamps for each point already converted.

However, it turns out that the JavaScript Date object does its own client-side timezone conversion based on the user’s system timezone settings. This means that if the default date on the graph is 10:00 GMT and your local system timezone is Paris, then JavaScript will automatically change that to 11:00 GMT.

This only works when the timestamp passed is in GMT. So it presents a problem when we have already done the timezone conversion server-side, i.e. the conversion will be calculated twice – first on the server, then again on the client.

We could allow JavaScript to handle timezones and perform all the conversions. However, this would result in messed up links, because we used data points to redirect the user to the actual snapshots.

Snapshots are provided in Unix timestamp format, so even if the JS did the conversion, the snapshot timestamp would still be incorrect. To completely remove the server side conversion and rely solely on JS would require more changes and a lot more JS within the interface.

UTC-based workaround

As such, we modified our getDate function to return the values in UTC—at least it is UTC as far as JS is concerned but in reality we’d have already done the conversion on the server. This effectively disables the JavaScript timezone conversion.

The following code snippet converts the Unix timestamp in JavaScript provided by the server into a date representation that we can use to display in the charts:

getDate: function(timestamp)
// Multiply by 1000 because JS works in milliseconds instead of the UNIX seconds
var date = new Date(timestamp * 1000);

var year = date.getUTCFullYear();
var month = date.getUTCMonth() + 1; // getMonth() is zero-indexed, so we'll increment to get the correct month number
var day = date.getUTCDate();
var hours = date.getUTCHours();
var minutes = date.getUTCMinutes();
var seconds = date.getUTCSeconds();

month = (month < 10) ? '0' + month : month;
day = (day < 10) ? '0' + day : day;
hours = (hours < 10) ? '0' + hours : hours;
minutes = (minutes < 10) ? '0' + minutes : minutes;
seconds = (seconds < 10) ? '0' + seconds: seconds;

return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes;

So this is how we handle timezone with JavaScript for the Server Density graphing engine. What is your experience with timezones in JavaScript?

Free eBook: 4 Steps to Successful DevOps

This eBook will show you how we i) hacked our on-call rotation to increase code resilience, ii) broke our infrastructure, on purpose, to debug quicker and increase uptime, and iii) borrowed practices from the healthcare and aviation industry, to reduce complexity, stress and fatigue. And speaking of stress and fatigue, we’ve devoted an entire chapter on how we placed humans at the centre of Ops, in order to increase their productivity and boost the uptime of the systems they manage. What are you waiting for, download your free copy now.

Help us speak your language. What is your primary tech stack?

What infrastructure do you currently work with?

  • Ivo Danihelka

    I understand the need to do the conversion on the server to align the graph with the day start.
    Do you store the data normally in UTC?
    Then it should be possible to send the timestamps also in UTC.

    It must be painful to remember that the produced timestamps are not in UTC.
    Unix timestamps are defined to be in UTC (except for a leap second).

  • How to convert GMT date time to other timezone time?

    • fudfighter

      The easiest way to follow is to convert your GMT datetime into UTC, then the UTC datetime into your target time zone. That way you only have to do lookups into the time zone table and not calculate the difference between time zones (with all the complications of adjusting for different daylight saving time offsets).

  • 相当不错,自愧不如!

  • karuna

    how to convert one time to another time zone .i want convert GMT to another time zone

Articles you care about. Delivered.

Help us speak your language. What is your primary tech stack?

Maybe another time