题意:如何将日期始终设置为东部时间,而不管用户的时区如何
问题背景:
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 时间转换。我在寻找一个 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日),而不受我计算机时区的影响呢
问题解决:
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!
不过,后端表示的区域字符串不会改变。这个答案的来源在于此帖。希望这能帮到你