问题描述
我有一个服务器在 Unix 时间给我的日期:1458619200000
I have a date given to me by a server in unix time: 1458619200000
注意:您标记为重复"的其他问题并未显示如何从 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)
那么我如何显示日期:1458619200000 ... 无论我的计算机的时区如何,始终处于东部时间(3 月 22 日)?
So how can I show the date: 1458619200000 ... to always be in eastern time (Mar 22) regardless of my computer's time zone?
推荐答案
您可以使用 getTimezoneOffset() 函数.例如,
You can easily take care of the timezone offset by using the getTimezoneOffset() function in Javascript. For example,
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!
这篇关于无论用户的时区如何,如何始终将日期设置为东部时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!