简体   繁体   中英

Issues with Time math in calculating dates

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM