时区上的Javascript转换日期

Javascript transform date on timezone

本文关键字:转换 日期 Javascript 时区      更新时间:2023-09-26

因此,我尝试占用应用程序范围内的时间,并将其显示在用户的时区中。这是我所做的,它正在发挥作用。然而,我相信有更好的方法可以做到这一点。起始时区将始终为Central。中心=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);
        }
    });
}

你说:

起始时区将始终为Central。中心=5。

这是不正确的。时区和时区偏移量不是一回事。美国中央时区在冬季使用UTC-6偏移,在夏令时生效的夏季使用UTC-5偏移。你不能硬编码这两个。另请参阅时区标记wiki中的"时区!=偏移"。

如果你需要在JavaScript中使用非本地时区,你需要一个库,比如我在这里列出的库。例如,以下是如何使用moment.js库的附加组件momenttimezone来实现这一点。

moment.tz('2014-10-02 01:23:00','America/Chicago').local().format('YYYY-MM-DD h:mm a')

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

我把放在一起的东西

// 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

现在

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"