0

I'm trying to understand when moving from using moment to using moment-timezone, do I need to replace references to the imported moment with references to moment-timezone instead? Does one replace the other (if I want to use moment-timezone features)?

By default, moment does not have a .tz() function, so if you do:

import moment from 'moment';
moment.tz.guess();

at this point tz is undefined. But if you import both:

import moment from 'moment';
import momentTimezone from 'moment-timezone';

Now both of these are valid:

moment.tz.guess();
momentTimezone.tz.guess();

Does this mean if you want to use the moment-timezone features you just import it and then you can continue to use moment as before, now with the added .tz property?

How does .tz get added to the original moment import just from importing the moment-timezone library?

Kevin Hooke
  • 2,583
  • 2
  • 19
  • 33
  • 1
    You can't use moment-timezone without moment. https://github.com/moment/moment-timezone/blob/develop/moment-timezone.js#L34 – Heretic Monkey Jul 11 '18 at 21:36
  • So is it correct to continue using moment from my import examples above after also importing moment-timezone, or should i replace all moment references to use moment-timezone instead? It's not clear from the docs what the relationship is between the two libraries, which is the reason why I'm asking whether you need to replace one with the other. – Kevin Hooke Jul 11 '18 at 21:50
  • 2
    Well, since you can't use moment-timezone without moment, logically you can't replace moment with moment-timezone :). moment-timezone "enhances" moment by adding time zone awareness to moment. So keep using moment as you always have, using the added time zone functionality as needed. To see how it works, look at the source code. You'll see that at the end it returns the `moment` global object. Your code `import momentTimezone from 'moment-timezone';` creates a new variable, `momentTimezone` which is a reference to the same object as `moment`. – Heretic Monkey Jul 12 '18 at 11:52

0 Answers0