最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript countdown timer: Calculate how many seconds until midnight EST - Stack Overflow

programmeradmin3浏览0评论

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 many seconds are left until midnight each day, EST (-5). Could someone please demonstrate how I might define that value and insert it for the "time" variable? I've seen other variations of this but I'm having trouble getting it to work for this specific script. Thank you in advance.

<script type="application/javascript">
var myCountdown1 = new Countdown({
time: 86400, // 86400 seconds = 1 day
width:200, 
height:55,  
rangeHi:"hour",
style:"flip"    // <- no ma on last item!
});
</script>

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 many seconds are left until midnight each day, EST (-5). Could someone please demonstrate how I might define that value and insert it for the "time" variable? I've seen other variations of this but I'm having trouble getting it to work for this specific script. Thank you in advance.

<script type="application/javascript">
var myCountdown1 = new Countdown({
time: 86400, // 86400 seconds = 1 day
width:200, 
height:55,  
rangeHi:"hour",
style:"flip"    // <- no ma on last item!
});
</script>
Share Improve this question asked Jan 31, 2014 at 14:34 sparecyclesparecycle 2,0585 gold badges31 silver badges59 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 8

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

  1. 暂无评论