Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3 - get current time and subtract by 2 hours

I'm trying to create two variables in d3, one that is the current hour & minute (in 00:00 format) and one that is simply the current time with the hour value subtracted by two. So, for instance: current time is 15:38, the other variable would be calculated to be 13:38.

Here's my current code for getting the current time (in coffeescript):

formatTime = d3.time.format("%H:%M")
currentTime = (d) -> formatTime(new Date())

Thanks in advance!

like image 327
Jackson Flint-Gonzales Avatar asked Sep 13 '13 22:09

Jackson Flint-Gonzales


1 Answers

Checkout d3.time.interval:

> d = new Date()
Fri Sep 13 2013 00:00:00 GMT-0400 (Eastern Daylight Time)
> twoHoursAgo = d3.time.hour.offset(d, -2)
Fri Sep 13 2013 17:09:04 GMT-0400 (Eastern Daylight Time)
> [twoHoursAgo, d].map(formatTime)
["17:09", "19:09"]
like image 115
Adam Pearce Avatar answered Nov 10 '22 08:11

Adam Pearce