Zeeshan
Zeeshan

Reputation: 1278

d3 changes time zone while parsing

I have date time as "2015-05-04 01:59:36". I am parsing these dates as follows-

var d= "2015-05-04 01:59:36";
var input_date_format = d3.time.format("%Y-%m-%d %H:%M:%S");

On parsing the date like this-

input_date_format.parse(d);

I get this date object-

2015-05-03T23:59:36.000Z

The date is getting offset by -2 hours. The system clock is at a timezone of +02:00. How to prevent this from happenig? Also if I run this d3 code on another system with different time zone, would it offset accordingly? I want the date to be preserved as it is while parsing.

How can it be done? Thanks

Upvotes: 0

Views: 256

Answers (1)

Hayden Schiff
Hayden Schiff

Reputation: 3330

Easiest solution would probably be to append "-0000" to all your timestamps, and then add "%Z" to the end of your format string. This way, you are explicitly telling D3 to use UTC for your timestamp.

So like this:

var d= "2015-05-04 01:59:36";
d += " -0000";
var input_date_format = d3.time.format("%Y-%m-%d %H:%M:%S %Z");

More info on D3 time formatting in their official docs.

Upvotes: 1

Related Questions