【发布时间】:2018-08-24 08:28:40
【问题描述】:
如何将 GMT 设置为 date.Now()?
var countDownDate = getNextDayOfWeek(new Date(),0,21);
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24)).toString();
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)).toString();
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)).toString();
var seconds = Math.floor((distance % (1000 * 60)) / 1000).toString();
// Add 0 when value are < 10
hours = (hours < 10) ? "0"+hours : hours;
minutes = (minutes < 10) ? "0"+minutes : minutes;
seconds = (seconds < 10) ? "0"+seconds : seconds;
var grb = jq("#grb");
// Display the result in the element with id="grb"
grb.html(days + "d " + hours + "h " + minutes + "m " + seconds + "s");
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
var one_hour = -60 * -60 * -1000;
if(distance < one_hour){
grb.html("GRB is finished!");
}else{
grb.html("GRB is open!");
}
}
}, 1000);
});
它一直在使用你电脑的时间,我想每次都使用一个时区来存储。
我已经尝试过 TimeZoneOffset,但没有成功,我们将不胜感激。
【问题讨论】:
-
getNextDayOfWeek 返回什么?使用间隔为 1000 的 setInterval 并不能保证它会以精确的一秒间隔运行。观察夏令时的日子并非都是 24 小时。 getTimezoneOffset 方法根据主机系统设置返回时区偏移量,它与 Date 对象无关。日期没有时区偏移属性,不能设置。
-
@Blutengel - 您的代码中没有任何内容适用于当地时间。请展示
getNextDayOfWeek的实现。您的问题可能会在那里得到解决。
标签: javascript timer timezone countdown