我真的需要使用 javascript 获取赫尔辛基和莫斯科的当前时间,但无论本地时区如何。我想知道该怎么做?也许有人有例子吗?
格式为:
2011 年 11 月 1 日 21:31:00
谢谢。
最佳答案
// create Date object for current location
d = new Date();
// convert to msec since Jan 1 1970
localTime = d.getTime();
// obtain local UTC offset and convertto msec
localOffset = d.getTimezoneOffset() * 60000;
// obtain UTC time in msec
utc = localTime + localOffset;
// obtain and add destination's UTC time offset
// for example, Paris
// which is UTC + 1.0 hours
offset = 1.0;
paris = utc + (3600000*offset);
// convert msec value to date string
nd = new Date(paris);
document.writeln("Paris time is " + nd.toLocaleString() + "<br>");
(抱歉,不知道赫尔辛基偏移量,可能是 2?)
请注意,getTimezoneOffset() 的负返回值表示当前位置早于 UTC,而正值表示该位置晚于 UTC。
[编辑]
这可能会更好:(请注意,您必须自己从接收到的 helsinki
变量
function getTZTime (tzOffset) {
local = new Date();
off = ( local.getTimezoneOffset() ) * 60 * 1000;
timeStamp = local.getTime() + off;
off += 1000 * 60 * 60 * tzOffset;
nd = new Date();
nd.setTime( timeStamp );
return (nd);
}
helsinki = getTZTime (2); // Helsinki is 2 TZ's from GMT
[/编辑]
关于javascript - 无论本地时区如何,都获得赫尔辛基本地时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7971813/