1

I have a countdown here which counts down to New Year's fine. But, for some reason the exact same code fails to work here when i put in a different date; it is exactly 1 month off.

Why is this happening?

Specifically, here is the code im using to get the days, hours, etc:

christmas = new Date(new Date().getFullYear(), 12, 25);

seconds = Math.floor((christmas - (new Date()))/1000);
minutes = Math.floor(seconds/60);
hours = Math.floor(minutes/60);
days = Math.floor(hours/24);

hours = hours-(days*24);
minutes = minutes-(days*24*60)-(hours*60);
seconds = seconds-(days*24*60*60)-(hours*60*60)-(minutes*60);
jackcogdill
  • 4,900
  • 3
  • 30
  • 48
  • possible duplicate of [How to subtract date/time in javascript?](http://stackoverflow.com/questions/4944750/how-to-subtract-date-time-in-javascript) – zerkms Dec 17 '12 at 02:32
  • I have it completely working for the first link, though why does it fail in the second one? – jackcogdill Dec 17 '12 at 02:34

1 Answers1

4

Your issue is here:

> christmas = new Date(new Date().getFullYear(), 12, 25);

Javascript months are zero indexed:

var christmas = new Date(new Date().getFullYear(), 11, 25);
RobG
  • 142,382
  • 31
  • 172
  • 209