D3 date range by hour; day repeats every 7 days

296 views Asked by At

So, I am working on a visualization that calculates the angle and distance of the sun at a specific location for each hour of every day in a year. The calculations take up about 550 lines of code, so I'll leave that code out. I'm using the D3 library to get the range of hours and relative day, month, and year of the range of dates. To get the range, I am using this line of code.

var hourRange = d3.time.hours(new Date(2013, 1, 1), new Date(2013, 12, 31));

After getting the calculations to work, I got very excited. Having 8736 objects holding the 9 values,

hourRange[i] = [hour, day, month, year, solar azimuth angle, solar elevation angle, EOT, solar declination angle, cosine of solar zenith angle]

... and finally starting the visualization process, I found that the day values went from 0-6, but instead of increasing until the end of the month, continued to repeat 0-6 until the end of the month and started the 0-6 value loop over again. Thinking this may be an issue from the calculations, I tried printing out the day value of the hour range directly and found the same issue happening. Reading the Time Interval API Reference for the D3 library, this seems to be an anomalous error.

Does anyone know why this is happening, as well as any solutions to get the range of hours and their relative dates without this issue of the day value repeating every week? Any insight to this will be greatly appreciated!

EDIT: To be clear. When type hourRange[500] in the console, it returns:

Date 2013-02-22T01:00:00.000Z

But, when I type hourRange[500].getDay() in the console, it returns:

4

when it should clearly be 22.

1

There are 1 answers

0
brettwbyron On

D3.time.getDay() returns the value of the day of the week. I needed to use D3.time.getDate() to get value of the day of the month.