我有这个代码根据用户计算机的时间计算直到午夜CST的毫秒数。
对于我所在的时区,now.getTimezoneOffset()
返回420,使tmzOfst
为60。
function millisToMidnight() {
var now = new Date();
var tmzOfst = (now.getTimezoneOffset())-360; //-360 minutes = CST
now.setHours(-(tmzOfst/60));// Adjust 'now' to CST time
var then = new Date(now); //make a var same as now
then.setHours(24, 0, 0, 0); //set to midnight
return (then - now); //calculate difference
}
然而,当我运行它(console.log'无处不在)时,我明白了:
Now = Tue Mar 07 2017 21:51:05 GMT-0700 (Mountain Standard Time)
tmzOfst = 120
Then = Mon Mar 06 2017 22:51:05 GMT-0700 (Mountain Standard Time)
正如您所看到的,正确地将时间更改为CST,但是,它最终也会更改日期。有更简单的方法吗?为什么它会改变这一天?
答案 0 :(得分:2)
如果你想调整小时数,你需要调整,而不是SET
<div class="featuredBoxes">
<div class="container">
<div class="row">
<!--column 1-->
<div class="col-md-4 col-sm-4 text-center col-1">
<div class="col-md-12 col-sm-12 call-to-action">
<span class="glyphicon glyphicon-wrench wrench" style="font-size:100px;"></span>
<h3>Wrench</h3>
<p>bla bla bla</p>
</div>
</div>
<!--column -2-->
<div class="col-md-4 col-sm-4 text-center col-2">
<div class="col-md-12 col-sm-12 call-to-action">
<span class="glyphicon glyphicon-phone phone" style="font-size:100px;"></span>
<h3>phone</h3>
<p>xa xa xa</p>
</div>
</div>
<!--column -3-->
<div class="col-md-4 col-sm-4 text-center col-3">
<div class="col-md-12 col-sm-12 call-to-action">
<span class="glyphicon glyphicon-globe globe" style="font-size:100px;"></span>
<h3>globe</h3>
<p>gud gud gud</p>
</div>
</div>
</div>
</div>
</div>
答案 1 :(得分:0)
这应该给你午夜的毫秒数。
state