简体   繁体   中英

The JavaScript world, living in a doubled future?

I am trying to get the exact amount of seconds, minutes etc. left to a specific date. This may sound stupid but why is the result doubled? That doesn't seem right, does it?

setInterval(function() {
    var startDate = new Date(),
        startDateTime = startDate.getTime(),
        endDate = new Date(2012, 5, 14),
        endDateTime = endDate.getTime();
    var timeLeft = endDateTime - startDateTime;
    var seconds = Math.round(timeLeft / 1000),
        minutes = Math.round(seconds / 60),
        hours = Math.round(minutes / 60),
        days = Math.round(hours / 24),
        weeks = Math.round(days / 7);
    console.log(weeks, days, hours, minutes, seconds);
}, 1000);​

The JavaScript Date constructor works just like the Java one--- Days and Years are 1-based, but the month is zero-based. So the date February 10th, 2015 is:

var aDate= new Date(2015,1,10);

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