设置 Date 对象的时区
Set the timezone of a Date object
我目前正在尝试开发一个倒计时页面。这是倒数计时器代码:
var clock;
$(document).ready(function() {
// Grab the current date
var currentDate = new Date();
// Set some date in the future. In this case, it's always Jan 1
var futureDate = new Date("July 01, 2015 22:00:00");
// Calculate the difference in seconds between the future and current date
var diff = futureDate.getTime() / 1000 - currentDate.getTime() / 1000;
if(diff < 0){
// Instantiate a countdown FlipClock
clock = $('.clock').FlipClock(0, {
clockFace: 'DailyCounter',
countdown: true
});
$('.message').html('Lets Go!!');
$('.Go').removeAttr("disabled");
$( "div.first" ).replaceWith( "<i style='color:red'>Lets Go!</i>" );
}
else{
// Instantiate a countdown FlipClock
clock = $('.clock').FlipClock(diff, {
clockFace: 'DailyCounter',
countdown: true,
callbacks: {
stop: function() {
$('.message').html('Lets Go!!');
$('.Go').removeAttr("disabled");
$( "div.first" ).replaceWith( "<i style='color:red'>Lets Go!</i>" );
}
}
});
}
});
问题是倒计时时间因时区而异。例如,澳大利亚用户的倒计时时间将比马来西亚用户(GMT+8)短三个小时。
如何standardize/set初始倒计时日期的时区为GMT+8,让不同时区的用户有相同的倒计时时间?
GMT 时间与 UTC 时间相同。
使用JavaScriptsetUTCDate()方法
例子
根据 UTC 设置月份中的第几天:
var d = new Date();
d.setUTCDate(15);
d 的结果将是:
Wed Jul 15 2015 10:47:28 GMT+0400 (Russian Standard Time)
基于our discussion,此示例代码可将任意时区时间转换为UTC+8时区时间。
var d = new Date();
d.setTime(d.getTime() + d.getTimezoneOffset() * 60 * 1000 /* convert to UTC */ + (/* UTC+8 */ 8) * 60 * 60 * 1000);
console.log('UTC+8 Time:', d);
这里有一个JSFiddle供参考
虽然控制台输出显示日期对象的时区不是UTC+0800,但是它们的日期值(年、月、日等...)都已转换为UTC+0800时间。
只是无法编辑日期对象的实际时区,但可以编辑它们的日期值以反映新时区,这就是我们在这里所做的。
我目前正在尝试开发一个倒计时页面。这是倒数计时器代码:
var clock;
$(document).ready(function() {
// Grab the current date
var currentDate = new Date();
// Set some date in the future. In this case, it's always Jan 1
var futureDate = new Date("July 01, 2015 22:00:00");
// Calculate the difference in seconds between the future and current date
var diff = futureDate.getTime() / 1000 - currentDate.getTime() / 1000;
if(diff < 0){
// Instantiate a countdown FlipClock
clock = $('.clock').FlipClock(0, {
clockFace: 'DailyCounter',
countdown: true
});
$('.message').html('Lets Go!!');
$('.Go').removeAttr("disabled");
$( "div.first" ).replaceWith( "<i style='color:red'>Lets Go!</i>" );
}
else{
// Instantiate a countdown FlipClock
clock = $('.clock').FlipClock(diff, {
clockFace: 'DailyCounter',
countdown: true,
callbacks: {
stop: function() {
$('.message').html('Lets Go!!');
$('.Go').removeAttr("disabled");
$( "div.first" ).replaceWith( "<i style='color:red'>Lets Go!</i>" );
}
}
});
}
});
问题是倒计时时间因时区而异。例如,澳大利亚用户的倒计时时间将比马来西亚用户(GMT+8)短三个小时。
如何standardize/set初始倒计时日期的时区为GMT+8,让不同时区的用户有相同的倒计时时间?
GMT 时间与 UTC 时间相同。 使用JavaScriptsetUTCDate()方法
例子
根据 UTC 设置月份中的第几天:
var d = new Date();
d.setUTCDate(15);
d 的结果将是:
Wed Jul 15 2015 10:47:28 GMT+0400 (Russian Standard Time)
基于our discussion,此示例代码可将任意时区时间转换为UTC+8时区时间。
var d = new Date();
d.setTime(d.getTime() + d.getTimezoneOffset() * 60 * 1000 /* convert to UTC */ + (/* UTC+8 */ 8) * 60 * 60 * 1000);
console.log('UTC+8 Time:', d);
这里有一个JSFiddle供参考
虽然控制台输出显示日期对象的时区不是UTC+0800,但是它们的日期值(年、月、日等...)都已转换为UTC+0800时间。
只是无法编辑日期对象的实际时区,但可以编辑它们的日期值以反映新时区,这就是我们在这里所做的。