D3.js parsing time with milliseconds not working?

Is there a workaround to get D3.js to parse data that contains milliseconds? I cannot get this to work:

var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%S").parse
parseDate("2011-01-01T12:14:35")
//that works

parseDate("2011-01-01T12:14:35.3456")
//returns null

      

+3


source to share


3 answers


If your dateTime strings are already in this format, you don't need d3 to parse it into an actual date object.

For example:



new Date("2011-01-01T12:14:35")
# Sat Jan 01 2011 04:14:35 GMT-0800 (PST)

      

results in the correct date object.

+5


source


Try looking for the formatting function d3.time.format.iso

shown on the wiki page: d3.time.format.iso .



+3


source


Include a format for parsing miliseconds that uses the% L source .

In your case:

var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%S").parse
parseDate("2011-01-01T12:14:35")    //that works

var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%S.%L").parse
parseDate("2011-01-01T12:14:35.345")    // works

      

Note that you have given 4 digits in milliseconds, which I think is incorrect if you are dealing with milliseconds. If you actually have 4 digits or 6 digits (microseconds) and you are only interested in milliseconds, you can filter out the last few digits. For example:

var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%S.%L000").parse
parseDate("2011-01-01T12:14:35.345000")    //that works

      

+1


source







All Articles