JavaScript倒数计时器:计算到美国东部标准时间午夜为止的秒数 [英] JavaScript countdown timer: Calculate how many seconds until midnight EST

查看:84
本文介绍了JavaScript倒数计时器:计算到美国东部标准时间午夜为止的秒数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用在JavaScript中运行的24小时倒数计时器.当前,它使用秒作为其基本度量.我在这里列出了86400,但我想计算出每天剩下的秒数,即美国东部标准时间(-5).有人可以演示如何定义该值并将其插入时间"变量中吗?我已经看到了它的其他变体,但是在使它适用于此特定脚本时遇到了麻烦.预先谢谢你.

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 comma on last item!
});
</script>

推荐答案

您可以从午夜的UNIX时间戳中减去现在的UNIX时间戳:

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

这里,您只需设置一个计时器,该计时器将UNIX时间戳记为午夜,然后将其从现在的UNIX时间戳记中减去,这将导致直到午夜的毫秒数.这就是执行脚本之前等待的毫秒数.

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.

这篇关于JavaScript倒数计时器:计算到美国东部标准时间午夜为止的秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆