Time arithmetic

We also get a full suite of time arithmetic functions that work with JavaScript's Date objects and follow a few simple rules:

  • d3.timeInterval, where "Interval" can be a second, a minute, an hour, and so on. It returns a new time interval. For instance, d3.timeHour will be an hour long.
  • d3.timeInterval(Date) is an alias for interval.floor(), which rounds Date down so that more specific units than the interval are set to zero.
  • interval.offset(Date, step) will move the date by a specified number of steps to the correct unit.
  • interval.range(Date_start, Date_stop) will return every interval between the two specified dates.

For instance, if you wanted to find the time an hour from now, you'd do this:

       > d3.timeHour.offset(new Date(), 1)
Sun Mar 19 2017 02:44:30 GMT+0100 (CET)

And find out it's getting really late and you should stop writing books about JavaScript and go to bed.

Want to do even more with time? Moment.js is a terrific library for accurately calculating things like time zones and the differences between two datestamps: http://momentjs.com.
..................Content has been hidden....................

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