moment-timezone.js:476 Moment Timezone没有亚洲/耶路撒冷的数据

时间:2018-05-08 12:06:45

标签: javascript momentjs

我正在使用moment-timezone.js

但我得到了这个例外:

moment-timezone.js:476 Moment Timezone has no data for Asia/Jerusalem

我去了references

我看到Asia/Jerusalem存在。

那么问题是什么呢?

toolFilters.filter('dateAccordingToTimeZone', function ($filter) {
    // Gets the number of milliseconds pass from 1970 and convert it to time according to given timezone, currently
    // supported “Asia/Jerusalem” and “America/Los_Angeles”
    return function (milliSeconds, timeZoneId) {
        if (milliSeconds == 0) {
            return "";
        }
        var timeInt = parseInt(milliSeconds, 10);
        var response = "";

        if (timeZoneId === undefined) {
            response = moment(timeInt);

        } else if (timeZoneId.includes("/")) {        //like "America/New_York"
            response = moment.tz(timeInt, timeZoneId);

        } else if (timeZoneId.includes("GMT")) {
            var offset = timeZoneId.substring(3);    //like "GMT-08:00"
            response = moment(timeInt).utcOffset(offset);

        } else {
            response = moment(timeInt);
        }

        var response = response.format('MMM D, YYYY H:mm:ss');
        console.log(response);
        return response;
}

我有这些进口商品:

<script src="bower_components/moment/moment.js"></script> 
<script src="bower_components/moment-timezone/moment-timezone.js"></script>

1 个答案:

答案 0 :(得分:2)

你需要moment-timezones-with-data.js lib

  

http://momentjs.com/timezone/

没有它,它没有所有国际时区,因为他们不想强制要求拥有所有时区数据。

  

使用数据时刻   

console.log(moment.tz('Asia/Jerusalem'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.16/moment-timezone-with-data.min.js"></script>

     

没有数据失败

  console.log(moment.tz('Asia/Jerusalem'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.16/moment-timezone.js"></script>

希望这有帮助!