使用时刻将时区特定日期转换为语言环境

时间:2019-01-03 17:10:09

标签: momentjs

我有以下时区数据:

{
    "Id": "Pacific Standard Time",
    "DisplayName": "(UTC-08:00) Pacific Time (US & Canada)",
    "StandardName": "Pacific Standard Time",
    "DaylightName": "Pacific Daylight Time",
    "BaseUtcOffset": "-08:00:00",
    "AdjustmentRules": [{
        "DateStart": "0001-01-01T00:00:00Z",
        "DateEnd": "2006-12-31T00:00:00Z",
        "DaylightDelta": "01:00:00",
        "DaylightTransitionStart": {
            "TimeOfDay": "0001-01-01T02:00:00Z",
            "Month": 4,
            "Week": 1,
            "Day": 1,
            "DayOfWeek": 0,
            "IsFixedDateRule": false
        },
        "DaylightTransitionEnd": {
            "TimeOfDay": "0001-01-01T02:00:00Z",
            "Month": 10,
            "Week": 5,
            "Day": 1,
            "DayOfWeek": 0,
            "IsFixedDateRule": false
        },
        "BaseUtcOffsetDelta": "00:00:00"
    }, {
        "DateStart": "2007-01-01T00:00:00Z",
        "DateEnd": "9999-12-31T00:00:00Z",
        "DaylightDelta": "01:00:00",
        "DaylightTransitionStart": {
            "TimeOfDay": "0001-01-01T02:00:00Z",
            "Month": 3,
            "Week": 2,
            "Day": 1,
            "DayOfWeek": 0,
            "IsFixedDateRule": false
        },
        "DaylightTransitionEnd": {
            "TimeOfDay": "0001-01-01T02:00:00Z",
            "Month": 11,
            "Week": 1,
            "Day": 1,
            "DayOfWeek": 0,
            "IsFixedDateRule": false
        },
        "BaseUtcOffsetDelta": "00:00:00"
    }],
    "SupportsDaylightSavingTime": true
}

以及以下日期:

2019-01-05T07:30:07Z

请暂时将其转换为当前的语言环境

en-GB

如何使用momentjs做到这一点?

问题似乎是我的日期格式与当下不兼容。

我准备了一个jsfiddle,其中包含我可以访问的变量:

http://jsfiddle.net/jimmyt1988/xe7ha5ms/1/

2 个答案:

答案 0 :(得分:1)

您的输入采用瞬间识别的ISO 8601格式。您可以使用moment.tz进行解析:

  

moment.tz构造函数采用与矩构造函数相同的所有参数,但将最后一个参数用作a time zone identifier

然后您可以使用tz()将其转换为所需的时区:

  

moment#tz更改器将更改时区并更新偏移量。

您可以使用locale()在矩对象上设置区域设置,然后使用format()显示值。

这里有一个代码示例:

console.log(
  moment.tz('2019-01-05T07:30:07', 'America/Los_Angeles') // parse input in given tz
    .tz('Europe/London') // Convert moment to desired zone
    .locale('en-gb') // set en-gb locale
    .format('LLLL') // display result
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script>

答案 1 :(得分:0)

通过使用utcOffset方法解决了这个问题:

http://jsfiddle.net/jimmyt1988/xe7ha5ms/2/

moment("2019-01-05T07:30:07Z")
.utcOffset("-08:00:00")
.format("DD/MM/YYYY HH:mm:ss");