Incorrect timezone in Firefox, compared to Safari, using javascript Date()

ぐ巨炮叔叔 提交于 2019-12-22 05:06:45

问题


The following code

var date = new Date();
console.log( date );

gives me

Sun Mar 06 2011 21:41:36 GMT+1300 (NZST) {}

in Firefox, but

Sun Mar 06 2011 21:40:51 GMT+1300 (NZDT)

in Safari (which is correct).

My system Date & Time is set to NZDT, so I'm wondering where firefox is getting its NZST from. Mind you, the UTC offset (+1300) is correct in both cases.

How can I get Firefox displaying the correct timezone: NZDT?


回答1:


You shouldn't rely on that output as it's different in other browsers (IE), instead you should use the getTimezoneOffset method.

var date = new Date;
console.log( date.getTimezoneOffset() );

The offset will change with day light savings but there are ways to work with this.




回答2:


If everything is how you want it except for the "NZST", you could just do a simple text replace:

console.log(date.toString().replace('NZST', 'NZDT'));

Note that this is really only simple fix for the display issue, it doesn't address the underlying cause.




回答3:


This was a bug that was fixed in Firefox v4 and later.



来源:https://stackoverflow.com/questions/5209587/incorrect-timezone-in-firefox-compared-to-safari-using-javascript-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!