本文介绍了获得赫尔辛基当地时间,无论当地时区如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的需要让赫尔辛基和莫斯科目前的时间使用javascript,但不管当地时区。我不知道该怎么做?也许有人有一个例子吗?

i really need to get Helsinki and Moscow current time using javascript but regardless of local timezone. I wonder how to do that? Maybe anyone has an example?

格式为:

2011年11月1日
21 :31:00

November 1, 201121:31:00

谢谢。

推荐答案

提前知道偏移量的作品:

The following function works when you know the offset ahead of time:

var MOSCOW_OFF = 4; // hours
var MONTHS = ["January", "February", "March", "April", "May", "June", "July", 
              "August", "September", "October", "November", "December"];

// desired format => November 1, 2011 21:31:00
function getLocalTime(offset) {
    var d = new Date();
    d.setTime((new Date().getTime()) + 
              (d.getTimezoneOffset() * 60 * 1000) + // local offset
              (1000 * 60 * 60 * offset)); // target offset
    return MONTHS[d.getMonth()] + " " + d.getDate() + ", " + 
            d.getFullYear() + " " + d.toTimeString().split(" ")[0];
}

getLocalTime(MOSCOW_OFF); // => "November 2, 2011 01:22:27"

以上将永远为莫斯科工作,更长的时间观察到夏令时,但是您需要知道一年中什么时候为赫尔辛基制定一个等效通用的解决方案。

The above will always work for Moscow, which no longer observes a Daylight Savings Time, but you'd need to be aware of what time of year it is to make an equivalently generic solution for Helsinki.

这篇关于获得赫尔辛基当地时间,无论当地时区如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 16:21