Given a string "2001-01-30"
, how can I parse it and obtain a JavaScript Date object, ideally using d3.js v4?
Asked
Active
Viewed 2,074 times
1

Michael Currie
- 13,721
- 9
- 42
- 58
1 Answers
2
To parse an ISO-8601-formatted date string, use the d3.js v4 method d3.isoParse
.
For example, in my location (Calgary, Alberta):
d3.isoParse("2001-01-30");
returns:
Mon Jan 29 2001 17:00:00 GMT-0700 (Mountain Standard Time)
To return a consistent end-of-day value, clear the time using Date.setHours(hour,min,sec,millisec)
:
let d = d3.isoParse("2001-01-30)
.setHours(0,0,0,0);
console.log(d);
Returns:
Mon Jan 29 2001 00:00:00 GMT-0700 (Mountain Standard Time)

Michael Currie
- 13,721
- 9
- 42
- 58