Moment-timezone: moment().tz.guess() is not a function

Created on 10 Apr 2017  路  6Comments  路  Source: moment/moment-timezone

I'm having some trouble getting this installed and working. I am using moment ~2.9.0 and moment-timezone ~0.5.13.

I have

require.config({
    paths: {
        moment: 'path/to/moment/moment',
        moment_tz: 'path/to/moment-timezone/builds/moment-timezone-with-data'
   },
   shim: {
        'moment_tz': {
            deps: ['moment'],
            exports: 'moment'
        },  
   }
});
define([
   'moment_tz'
], function(moment) {
  ...
});

Interestingly it looks like moment is being installed into global scope instead of being made available through the define.

screen shot 2017-04-10 at 9 35 33 am

Any ideas on how to resolve this?

Most helpful comment

I think this is just a syntax error. No parens after moment. Put them after guess instead.

moment.tz.guess()

Edited - because I had an error too at first.

All 6 comments

Does this help?
http://momentjs.com/timezone/docs/#/use-it/require-js/

Also, while moment 2.9.0 is the minimum that moment-timezone supports, you should probably use the latest 2.18.1

Thanks @mj1856, I should have mentioned in the issue, that was the first thing I tried:

require.config({
    paths: {
        ...
        moment: 'path/to/moment/moment',
   }
});
define([
   '../../path/to/moment-timezone/builds/moment-timezone-with-data',
], function(moment) {
  ...
});

But I have the same results. Not sure if it is relevant, but I am also using bootstrap_timepicker which depends on moment. I have also tried moment 2.18.1 with a few different configurations, and haven't been able to get that working either.

I think this is just a syntax error. No parens after moment. Put them after guess instead.

moment.tz.guess()

Edited - because I had an error too at first.

Missed that. lol.

Also, I found that this works too:

require.config({
  paths: {
    'moment': 'path/to/moment',
    'moment-timezone': 'path/to/moment-timezone-with-data',
  }
});
require(['moment-timezone'], function (moment) {

    var s = moment().tz("America/New_York").format();

    console.log(s);

});

https://jsfiddle.net/gzyhnxm8/

Well, that's embarrassing. Thanks both of you for your help. Sorry for the noise.

No problem. Glad we could help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

guidsen picture guidsen  路  4Comments

edu2004eu picture edu2004eu  路  6Comments

casper5822 picture casper5822  路  5Comments

garryyao picture garryyao  路  4Comments

jsg2021 picture jsg2021  路  3Comments