javascript 设置 Date 对象的时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31154023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:22:06  来源:igfitidea点击:

Set the timezone of a Date object

javascriptdatetimezone

提问by Marcus Tan

I'm currently trying to develop a countdown timer page. Here is the countdown timer code:

我目前正在尝试开发一个倒数计时器页面。这是倒数计时器代码:

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

});

The problem is that the countdown time varies per timezone. For example, a user in Australia will have a three-hour-shorter countdown time than that of a user from Malaysia (GMT+8).

问题是倒计时时间因时区而异。例如,澳大利亚用户的倒计时时间将比马来西亚用户 (GMT+8) 的倒计时时间短三小时。

How can I standardize/set the initial countdown date's timezone to GMT+8 so that users in different timezones have the same countdown time?

如何将初始倒计时日期的时区标准化/设置为 GMT+8,以便不同时区的用户具有相同的倒计时时间?

采纳答案by usandfriends

Based on our discussion, this example code works to convert any timezone time into UTC+8 timezone time.

根据我们的讨论,此示例代码可将任何时区时间转换为 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);

Here is a JSFiddlefor reference.

这是一个JSFiddle供参考。

Although the console output shows that the timezones of the date objects are not UTC+0800, their date values (year, month, date, etc...) have all been converted into UTC+0800 time.

尽管控制台输出显示日期对象的时区不是 UTC+0800,但它们的日期值(年、月、日等)都已转换为 UTC+0800 时间。

It is just not possible to edit the actual timezone of date objects, but it is possible to edit their date values to reflect the new timezone, and that is what we are doing here.

无法编辑日期对象的实际时区,但可以编辑它们的日期值以反映新时区,这就是我们在这里所做的。

回答by katerinkadar

GMT time is the same as UTC time. Use JavaScript setUTCDate()Method

GMT 时间与 UTC 时间相同。使用 JavaScript setUTCDate()方法

Example

例子

Set the day of the month, according to UTC:

根据 UTC 设置月份中的日期:

var d = new Date();
d.setUTCDate(15);

The result of d will be:

d 的结果将是:

Wed Jul 15 2015 10:47:28 GMT+0400 (Russian Standard Time)

2015 年 7 月 15 日星期三 10:47:28 GMT+0400(俄罗斯标准时间)