d3.js v4 timeParse() not working?

Olivier Krull picture Olivier Krull · Apr 4, 2017 · Viewed 10k times · Source

I saw a lot of examples doing the same, but the d3.timeParse() function just doesn't work as expected in my code and returns null.

let parseDate = d3.timeParse("%Y-%m-%d");

let now = new Date();

console.log(now)//returns js date object

let parsedNow = parseDate(now);

console.log(parsedNow) //returns null

See this fiddle for running example.

Any Ideas what I'm doing wrong here?

Help would be greatly appreciated.

Answer

Cyril Cherian picture Cyril Cherian · Apr 4, 2017

You are trying to parse a date object which is incorrect, I think you want to format a date object into "%Y-%m-%d"

so instead of this

let parseDate = d3.timeParse("%Y-%m-%d");//timeParse is incorrect

it should have been

let parseDate = d3.timeFormat("%Y-%m-%d");//timeFormat a date object into %Y-%m-%d

working code here