JavaScript倒计时器:计算美国东部时间午夜前的秒数

时间:2014-01-31 14:34:20

标签: javascript

我正在使用在JavaScript中运行的24小时倒数计时器。目前,它使用秒作为基础测量。我在这里列出了86400但是我想计算每天午夜剩下的秒数,EST(-5)。有人可以请证明我如何定义该值并将其插入“时间”变量?我已经看到了其他的变化,但我无法让它为这个特定的脚本工作。提前谢谢。

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

1 个答案:

答案 0 :(得分:7)

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

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时间戳中减去它,这将产生直到午夜的毫秒数。这是在执行脚本之前等待的毫秒数。

相关问题