Gerald
Gerald

Reputation: 11

Why do I need a 00 time zone offset to display values correctly in d3v4?

I'm trying to parse the following dates with d3:

20-06-18 01-02-35
20-06-18 09-13-01

using:

var parseTime = d3.timeParse("%d-%m-%y %H-%M-%S");

However, the console.log shows me these parsed values:

Date 2018-06-19T23:02:35.000Z
Date 2018-06-20T07:13:01.000Z

which are off by two hours.


Okay, I thought, after all, I'm sitting in the CEDT, I should probably add an offset. So I added -02 to the dates to parse and %Z to the timeParse argument, but it's still not what I want:

var parseTime = d3.timeParse("%d-%m-%y %H-%M-%S %Z");
parseTime("20-06-18 01-02-35 -02") // Date 2018-06-20T03:02:35.000Z
parseTime("20-06-18 09-13-01 -02") // Date 2018-06-20T11:13:01.000Z

Long story short, asking for a -00 offset gives me the correct values. But it seems a bit redundant to me. Why does this happen?

EDIT: Only happens in Firefox, Chrome gives me

Wed Jun 20 2018 09:13:01 GMT+0200

Still no idea why, tho.

Upvotes: 1

Views: 305

Answers (1)

Xavier Guihot
Xavier Guihot

Reputation: 61666

You're probably looking for d3.utcParse instead of d3.parseTime in order to avoid using the local time zone:

var parser = d3.utcParse("%d-%m-%y %H-%M-%S");
console.log(parser("20-06-18 01-02-35"));
<script src="https://d3js.org/d3.v5.min.js"></script>

And to go back to string, you can use d3.utcFormat instead of d3.timeFormat.

Upvotes: 1

Related Questions