更改 Javascript 的 getTimezoneOffset 以返回不同的时区偏移量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8602047/
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
change the Javascript's getTimezoneOffset to return a different time zone offset?
提问by zhkzyth
Is there a way to change the Javascript's getTimezoneOffset to return a different time zone offset?
有没有办法更改 Javascript 的 getTimezoneOffset 以返回不同的时区偏移量?
Fox example,my local time zone is GMT-8,but i want the Date.getTimezoneOffset() to return GMT-1,so i do this as follow:
Fox 示例,我的本地时区是 GMT-8,但我希望 Date.getTimezoneOffset() 返回 GMT-1,所以我这样做如下:
var timeZone1 = new Date(Date.parse("2011-01-01T00:00:00-01:00"));
document.write("The local time zone is: GMT " + timeZone1.getTimezoneOffset()/60);
//show:The local time zone is: GMT-8
so,How to make it shows GMT-1?
那么,如何让它显示 GMT-1?
Refference links:
参考链接:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
回答by Matt
I don't think javascript has a direct way to do this, soooo you can either calculate it yourself or use a date library.
我不认为 javascript 有直接的方法来做到这一点,所以你可以自己计算或使用日期库。
http://code.google.com/p/datejs/datejs is a pretty nice library.
http://code.google.com/p/datejs/datejs 是一个非常好的库。
To calculate yourself try,
要计算自己尝试,
function calcTime(offset) {
d = new Date();
//Deal with dates in milliseconds for most accuracy
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
newDateWithOffset = new Date(utc + (3600000*offset));
//This will return the date with the locale format (string), or just return newDateWithOffset
//and go from there.
return newDateWithOffset.toLocaleString();
}
Then pass the offset as like +1 or -5, etc.. as in calcTime('-1')
然后将偏移量传递为 +1 或 -5 等。如 calcTime('-1')
回答by tfont
Quick and fast solution using pure JavaScript.
使用纯 JavaScript 的快速解决方案。
var currentTime = new Date();
var currentTimezone = currentTime.getTimezoneOffset();
currentTimezone = (currentTimezone/60) * -1;
if (currentTimezone !== 0)
{
utc = currentTimezone > 0 ? ' +' : ' ';
utc += currentTimezone
}
alert(utc);
回答by homer242
getTimezoneOffset()
function always returns the timezone offset of your machine.
getTimezoneOffset()
函数始终返回您机器的时区偏移量。