Search code examples
timezonedygraphs

Dygraphs timestamp with timezone


I know there was a lot of discussion about datetime parsing in dygraphs and problems with Javascript. But still, I have a problem how to correctly pass datetime.

I'm using dygraph to show data (points with 5 seconds interval) and it's crucial to have correct datetime. But if I pass datetime as timestamp (1401580800) or as ISO 8601 (2014-06-01T00:00:00+00:00), I always get datetime modified to my local time.

My question is, how to correctly pass the datetime to dygraph so datetime doesn't change?


Solution

  • Dygraphs use Javascript Dates, which display according to your local timezone (unfortunately, there's no way to display them using an arbitrary timezone).

    One solution is to modify the data - I've encountered a similar problem in my own work, and I made a small helper function to make fake Javascript UTC dates (basically ending up with a Javascript date that's actually the wrong time but shows up looking correct when displayed in local time). This worked for my application, which used moments (http://momentjs.com/) to represent dates everywhere unless required by some other library to use Javascript's Dates.

    Another (probably more correct) solution is to modify Dygraph's functions to display the right things, as demonstrated in the answer to this post: Dygraph showing dates in an arbitrary Timezone