Reputation: 2520
In this block,
https://bl.ocks.org/mbostock/1667367
I see this
var parseDate = d3.time.format("%b %Y").parse;
which is used later like this
function type(d) {
d.date = parseDate(d.date);
d.price = +d.price;
return d;
}
Why is .parse
stuck on the end when every other example I've seen doesn't need it?
When I try to experiment with that syntax in other REPLs, I get a type error:
parseDate.parse is not a function
Upvotes: 0
Views: 87
Reputation: 15982
It parses a string to a date.
https://github.com/mbostock/d3/wiki/Time-Formatting#parse
In the context of var parseDate = d3.time.format("%b %Y").parse;
parseDate.parse is not a function
makes sense because parseDate
is the function, not parseDate.parse
. You call it like parseDate('1/1/2016')
, not parseDate.parse('1/1/2016')
You don't need to stick parse
at the end.
You can do
var parseDate = d3.time.format("%b %Y")
and then
parseDate.parse('1/1/2015)
.
Example from D3 docs.
var format = d3.time.format("%Y-%m-%d");
format.parse("2011-01-01"); // returns a Date
format(new Date(2011, 0, 1)); // returns a string
This allows you to format and parse a string. The first example only allows you to parse.
Upvotes: 2