时区上的Javascript转换日期

时区上的Javascript转换日期,javascript,datetime,timezone,timezone-offset,Javascript,Datetime,Timezone,Timezone Offset,因此,我尝试获取应用程序范围内的时间,并将其显示在用户的时区中。这就是我所做的工作,它正在发挥作用。然而,我相信有更好的方法来做到这一点。起始时区将始终处于中心位置。中环=5。我只关心美国的时区,但如果有一个简单的方法在全世界实施它,我也很失望。我正在使用的文本标签将显示为下午4:00,这就是为什么有这么多子字符串 function timeZones() { var timezone = new Date().getTimezoneOffset(); timezone = ti

因此,我尝试获取应用程序范围内的时间,并将其显示在用户的时区中。这就是我所做的工作,它正在发挥作用。然而,我相信有更好的方法来做到这一点。起始时区将始终处于中心位置。中环=5。我只关心美国的时区,但如果有一个简单的方法在全世界实施它,我也很失望。我正在使用的文本标签将显示为下午4:00,这就是为什么有这么多子字符串

function timeZones() {
    var timezone = new Date().getTimezoneOffset();
    timezone = timezone / 60;
    //STARTING TIMEZONE WILL ALWAYS BE CENTRAL
    var difference = 5 - timezone;
    $(".time-zone").each(function () {
        var a = $(this).text();
        var hour = a.substring(0, a.indexOf(":") - 1);
        hour = parseInt(a);
        var yourTime;
        //East Coast
        if (difference == 1) {
            hour = hour + difference;
            if (hour == 12) {
                yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("A") - 1) + "PM";
            }
            else if (hour > 12) {
                hour = hour - 12;
                yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("A") - 1) + "PM";
            }
            else {
                yourTime = hour + a.substring(a.indexOf(":"));
            }
            $(this).text(yourTime);
        }
        //Mountain
        if (difference == -1) {
            hour = hour + difference;
            if (hour == 0) {
                hour = 12;
                yourTime = hour + a.substring(a.indexOf(":"));
            }
            else if (hour < 0) {
                hour = 12 + hour;
                yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("P") - 1) + "AM";
            }
            else {
                yourTime = hour + a.substring(a.indexOf(":"));
            }
            $(this).text(yourTime);
        }
        //West Coast
        if (difference == -2) {
            hour = hour + difference;
            if (hour == 0) {
                hour = 12;
                yourTime = hour + a.substring(a.indexOf(":"));
            }
            else if (hour < 0) {
                hour = 12 + hour;
                yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("P") - 1) + "AM";
            }
            else {
                yourTime = hour + a.substring(a.indexOf(":"));
            }
            $(this).text(yourTime);
        }
        //Alaska
        if (difference == -3) {
            hour = hour + difference;
            if (hour == 0) {
                hour = 12;
                yourTime = hour + a.substring(a.indexOf(":"));
            }
            else if (hour < 0) {
                hour = 12 + hour;
                yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("P") - 1) + "AM";
            }
            else {
                yourTime = hour + a.substring(a.indexOf(":"));
            }
            $(this).text(yourTime);
        }
    });
}
功能时区(){
var timezone=new Date().getTimezoneOffset();
时区=时区/60;
//起始时区将始终处于中心位置
var差=5-时区;
$(“.timezone”)。每个(函数(){
var a=$(this.text();
var hour=a.substring(0,a.indexOf(“:”)-1);
小时=parseInt(a);
用你的时间;
//东海岸
如果(差=1){
小时=小时+差;
如果(小时==12){
yourTime=hour+a.substring(a.indexOf(“:”),a.indexOf(“a”)-1)+“PM”;
}
否则,如果(小时>12){
小时=小时-12;
yourTime=hour+a.substring(a.indexOf(“:”),a.indexOf(“a”)-1)+“PM”;
}
否则{
yourTime=hour+a.substring(a.indexOf(“:”);
}
$(this).text(yourTime);
}
//山
如果(差异==-1){
小时=小时+差;
如果(小时==0){
小时=12;
yourTime=hour+a.substring(a.indexOf(“:”);
}
否则如果(小时<0){
小时=12+小时;
yourTime=hour+a.substring(a.indexOf(“:”),a.indexOf(“P”)-1)+“AM”;
}
否则{
yourTime=hour+a.substring(a.indexOf(“:”);
}
$(this).text(yourTime);
}
//西海岸
如果(差异==-2){
小时=小时+差;
如果(小时==0){
小时=12;
yourTime=hour+a.substring(a.indexOf(“:”);
}
否则如果(小时<0){
小时=12+小时;
yourTime=hour+a.substring(a.indexOf(“:”),a.indexOf(“P”)-1)+“AM”;
}
否则{
yourTime=hour+a.substring(a.indexOf(“:”);
}
$(this).text(yourTime);
}
//阿拉斯加
如果(差异==-3){
小时=小时+差;
如果(小时==0){
小时=12;
yourTime=hour+a.substring(a.indexOf(“:”);
}
否则如果(小时<0){
小时=12+小时;
yourTime=hour+a.substring(a.indexOf(“:”),a.indexOf(“P”)-1)+“AM”;
}
否则{
yourTime=hour+a.substring(a.indexOf(“:”);
}
$(this).text(yourTime);
}
});
}

我拼凑的东西

// timezone_out optional = local timezone
function timeConversionGenerator(timezone_in, timezone_out) {
    if (undefined === timezone_out)
        timezone_out = new Date().getTimezoneOffset();
    var d = timezone_in - timezone_out;
    return function (time) {
        var hh = time.hh,
            mm = time.mm + d,
            ss = time.ss;
        while (mm <   0) mm += 60, hh -= 1;
        while (mm >= 60) mm -= 60, hh += 1;
        while (hh <   0) hh += 24;
        hh %= 24;
        return {
            hh: hh,
            mm: mm,
            ss: ss
        };
    }
}

var c = timeConversionGenerator(5 * 60); // 5 hours behind UTC to local time
然后

函数到amString(时间){
返回((time.hh%12)| | 12)
+ ':'
+(time.mm<10?'0':'')+time.mm
+''+(time.hh<12?'AM':'PM');
}
//所以
toamString(c({hh:13,mm:18,ss:0}));//“下午7:18”
你说过:

起始时区将始终处于中心位置。中环=5

这是不正确的。时区和时区偏移量不是一回事。美国中央时区在冬季使用UTC-6偏移,在夏季生效时使用UTC-5偏移。这两种代码都不能硬编码。另请参见中的“时区!=偏移”

如果需要在JavaScript中处理非本地时区,则需要一个库,如我列出的库。例如,下面是如何使用-这是库的一个附加组件来实现这一点


在上面的示例中,解析芝加哥的本地日期/时间(美国中央时间)。然后将其转换为用户浏览器运行的任何时区(使用
local
功能)。然后根据指定的格式将其格式化为字符串。

我认为您应该查看片刻时区库,而不是尝试实现此功能。这会帮你省去很多头疼的事。外面的人太多了。你会推荐哪一个?这就是我所说的:3年后,在我的编程生涯中,我学会了始终使用Momentjs,不再试图重新设计轮子。老实说,这个旧代码的问题在于我当时所在的公司没有将日期存储为UTC。
c({
    hh: 13,
    mm: 18,
    ss: 0
});
/* converted to my local time (British Summer Time)
{
   hh: 19,
   mm: 18,
   ss: 0
}
*/
function toAMPMString(time) {
    return ((time.hh % 12) || 12)
        + ':'
        + (time.mm < 10 ? '0' : '') + time.mm
        + ' ' + (time.hh < 12 ? 'AM' : 'PM');
}

// so
toAMPMString(c({ hh: 13, mm: 18, ss: 0})); // "7:18 PM"
moment.tz('2014-10-02 01:23:00','America/Chicago').local().format('YYYY-MM-DD h:mm a')