Formatting

You can create a new formatter by giving d3.timeParse() a format string. You can then use it to parse strings into Date objects.

The whole language is explained in the documentation of D3, but let's look at a few examples:

> format = d3.timeParse('%Y-%m-%d')
> format('2015-12-14')
Mon Dec 14 2015 00:00:00 GMT+0100 (CET)

We defined a new formatter with d3.timeParse() and provided a string mapping to year-month-day. We then parsed a date, as they often appear in datasets; this gave us a proper date object with default values for hours, minutes, and seconds.

d3.timeFormat() works in another way:

> format = d3.timeFormat('%Y-%m-%d')
> format(new Date());
"2017-03-12"

You can find the full ISO standard time formatter at d3.isoFormat. This often comes in handy, given that most databases use it by default, as does the toString() method of Date.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset