Javascript 无论用户的时区如何,如何始终将日期设置为东部时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36206260/
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-08-23 18:45:38  来源:igfitidea点击:

How to set date always to eastern time regardless of user's time zone

javascriptdatetimetimezone

提问by foreyez

I have a date given to me by a server in unix time: 1458619200000

我有一个服务器在 unix 时间给我的日期:1458619200000

NOTE: the other questions you have marked as "duplicate" don't show how to get there from UNIX TIME. I am looking for a specific example in javascript.

注意:您标记为“重复”的其他问题没有显示如何从 UNIX TIME 到达那里。我正在寻找 javascript 中的特定示例。

However, I find that depending on my timezone I'll have two different results:

但是,我发现根据我的时区,我会得到两种不同的结果:

d = new Date(1458619200000)
Mon Mar 21 2016 21:00:00 GMT-0700 (Pacific Daylight Time)

// Now I set my computer to Eastern Time and I get a different result.

// 现在我将我的计算机设置为东部时间,我得到了不同的结果。

d = new Date(1458619200000)
Tue Mar 22 2016 00:00:00 GMT-0400 (Eastern Daylight Time)

So how can I show the date: 1458619200000 ... to always be in eastern time (Mar 22) regardless of my computer's time zone?

那么我怎样才能显示日期:1458619200000 ... 总是在东部时间(3 月 22 日),无论我的计算机时区如何?

回答by Shekhar Chikara

You can easily take care of the timezone offset by using the getTimezoneOffset()function in Javascript. For example,

您可以使用Javascript 中的getTimezoneOffset()函数轻松处理时区偏移。例如,

var dt = new Date(1458619200000);
console.log(dt); // Gives Tue Mar 22 2016 09:30:00 GMT+0530 (IST)

dt.setTime(dt.getTime()+dt.getTimezoneOffset()*60*1000);
console.log(dt); // Gives Tue Mar 22 2016 04:00:00 GMT+0530 (IST)

var offset = -300; //Timezone offset for EST in minutes.
var estDate = new Date(dt.getTime() + offset*60*1000);
console.log(estDate); //Gives Mon Mar 21 2016 23:00:00 GMT+0530 (IST)

Though, the locale string represented at the back will not change. The source of this answer is in this post. Hope this helps!

但是,后面表示的语言环境字符串不会改变。这个答案的来源在这篇文章中。希望这可以帮助!

回答by Ashwin Balamohan

Moment.js (http://momentjs.com/timezone) is your friend.

Moment.js ( http://momentjs.com/timezone) 是你的朋友。

You want to do something like this:

你想做这样的事情:

var d = new Date(1458619200000);
var myTimezone = "America/Toronto";
var myDatetimeFormat= "YYYY-MM-DD hh:mm:ss a z";
var myDatetimeString = moment(d).tz(myTimezone).format(myDatetimeFormat);

console.log(myDatetimeString); // gives me "2016-03-22 12:00:00 am EDT"

回答by Hasan Badshah

For dayLight saving, Eastern time become 4 hours behind UTC thats why its offset is -4x60 = -240 minutes. So when Day light is not active the offset will be -300. easternTimeOffset variable value is the key point to be noted here. Kindly see this code in action in attached image.

对于夏令时,东部时间比 UTC 晚 4 小时,这就是为什么它的偏移量为 -4x60 = -240 分钟。因此,当日光未激活时,偏移量将为 -300。easternTimeOffset 变量值是这里需要注意的关键点。请在附加图片中查看此代码的实际效果。

       var offset = new Date().getTimezoneOffset();// getting offset to make time in gmt+0 zone (UTC) (for gmt+5 offset comes as -300 minutes)
        var date = new Date();
        date.setMinutes ( date.getMinutes() + offset);// date now in UTC time

        var easternTimeOffset = -240; //for dayLight saving, Eastern time become 4 hours behind UTC thats why its offset is -4x60 = -240 minutes. So when Day light is not active the offset will be -300
        date.setMinutes ( date.getMinutes() + easternTimeOffset);

You can see this code in action here in this image

您可以在此图像中看到此代码的运行情况