I had a need to iterate over days of a year today in JavaScript.
I figured you could just add “one day” to the date, and that turns out to be true.
This is the test program I wrote to make sure the incrementing worked properly.
var year = 2008;
var endymd = new Date(year+1, 0, 1, 0, 0, 0).getTime();
var days = 0;
for (var ymd=new Date(year, 0, 1, 0, 0, 0);ymd.getTime()<endymd; md.setDate(ymd.getDate()+1)){
days++;
}
console.log(days);
The answer is 366, which means that leap year was handled correctly.