简体   繁体   中英

Moment.js not working with external javascript file

The error described below is reproduced here: https://jsfiddle.net/cxhvr3Lq/


I am using jQuery, bootstrap, moment, and my own javascript file myjs.js. I created a minimum example where my custom javascript is not working. The code in myjs.js is:

function adjust_times_user_time_zone() {
    console.log(moment.tz("2015-08-05T00:00:00+01:00", "Europe/London"));
};

$(document).ready(function () {
    adjust_times_user_time_zone();
});

but this throws the following error, which is incomprehensible to me.

jquery.min.js:2 jQuery.Deferred exception: Cannot read property 'apply' of undefined TypeError: Cannot read property 'apply' of undefined
    at h.utcOffset (https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.33/moment-timezone-with-data.js:664:15)
    at Function.moment.updateOffset (https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.33/moment-timezone-with-data.js:623:9)
    at h.fn.tz (https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.33/moment-timezone-with-data.js:638:12)
    at Function.tz (https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.33/moment-timezone-with-data.js:568:7)
    at adjust_times_user_time_zone (file:///Users/js/myjs.js:37:24)
    at HTMLDocument.<anonymous> (file:///Users/js/myjs.js:41:5)
    at e (https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:30005)
    at t (https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:30307) undefined

jquery.min.js:2 Uncaught TypeError: Cannot read property 'apply' of undefined
    at h.utcOffset (moment-timezone-with-data.js:664)
    at Function.moment.updateOffset (moment-timezone-with-data.js:623)
    at h.fn.tz (moment-timezone-with-data.js:638)
    at Function.tz (moment-timezone-with-data.js:568)
    at adjust_times_user_time_zone (myjs.js:37)
    at HTMLDocument.<anonymous> (myjs.js:41)
    at e (jquery.min.js:2)
    at t (jquery.min.js:2)

I am loading jquery, bootstrap, moment, and myjs in that sequence. Any idea what is wrong?

moment's version needs to be at least 2.9.0. From the docs:

To use moment-timezone, you will need moment@2.9.0+, moment-timezone.js, and the moment-timezone data.

https://momentjs.com/timezone/docs/

Here is a working fiddle: https://jsfiddle.net/75oLy36p/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM