Home > Blockchain >  d3 Improperly parsing time from timestamp
d3 Improperly parsing time from timestamp

Time:09-18

I am working with d3 to plot values over time, I have a timestamp in the format of 2021-07-01 05:00:00

I am trying to use d3.timeParse with the following code:

d3.timeParse("%Y-%m-%d %H:%I:%S")("2021-07-01 05:00:00")

which returns

Thu Jul 01 2021 00:00:00 GMT-0400 (Eastern Daylight Time)

so it thinks that it parses successfully but if you notice it doesn't parse the hour correctly, however, if I change the minute to this 2021-07-01 05:15:00 it returns

Thu Jul 01 2021 15:00:00 GMT-0400 (Eastern Daylight Time)

I don't understand why the minute position is being parsed as the hour and the hour is being ignored. I have tested this all in the console.

CodePudding user response:

It appears you are parsing the hour twice: both H and I represent hours (24 hour and 12 hour representations respectively). This likely means you are overwriting the first hour parsed with the second hour, hence your results : the minute position is being parsed as the hour and the hour is being ignored.

You are likely looking for %H:%M for hours and minutes instead.

  • Related