JavaScript countdown timer: Calculate how many seconds until midnight EST

后端 未结 1 1846
南方客
南方客 2021-01-15 00:44

I\'m using a 24 hour countdown timer that runs in JavaScript. Currently, it uses seconds as its base measurement. I have 86400 listed here but I would like to calculate how

相关标签:
1条回答
  • 2021-01-15 00:59

    You can subtract the UNIX timestamp of now, from the UNIX timestamp of midnight:

    var now = new Date();
    var night = new Date(
        now.getFullYear(),
        now.getMonth(),
        now.getDate() + 1, // the next day, ...
        0, 0, 0 // ...at 00:00:00 hours
    );
    var msTillMidnight = night.getTime() - now.getTime();
    var myCountdown1 = new Countdown({
        time: msTillMidnight / 1000, // divide by 1000 to get from ms to sec, if this function needs seconds here.
        width:200, 
        height:55,  
        rangeHi:"hour",
        style:"flip"    // <- no comma on last item!
    });
    

    Here you simply set a timer that takes the UNIX timestamp of midnight, and subtracts it from the UNIX timestamp of now, which will result in the amount of milliseconds until midnight. That is the amount of milliseconds it will then wait before executing the script.

    0 讨论(0)
提交回复
热议问题