Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moment.tz.setDefault() is global and shared by all modules. #3754

Closed
jontelm opened this issue Feb 7, 2017 · 11 comments · Fixed by moment/momentjs.com#648
Closed

moment.tz.setDefault() is global and shared by all modules. #3754

jontelm opened this issue Feb 7, 2017 · 11 comments · Fixed by moment/momentjs.com#648

Comments

@jontelm
Copy link

jontelm commented Feb 7, 2017

Default timezone is shared between all modules:

server.js

var moment = require('moment-timezone');
console.log('sets timezone to Europe/Stockholm and locale to sv');
moment.tz.setDefault('Europe/Stockholm');
moment.locale('sv');

var moduleA = require('./moduleA');
var moduleB = require('./moduleB');

moduleA.js

var moment = require('moment-timezone');

setInterval(function () {
  console.log('Moment in moduleA', moment().format('MMMM Do YYYY, h:mm:ss a'));
}, 100)

moduleB.js

var moment = require('moment-timezone');

setInterval(function () {
  console.log('Moment in moduleB', moment().format('MMMM Do YYYY, h:mm:ss a'));
}, 100)

setInterval(function () {
  console.log('Changing timezone in moduleB  America/New_York locale en');
  moment.tz.setDefault("America/New_York");
  moment.locale('en');
}, 3000);

Output after 3 seconds:

Moment in moduleA februari 8e 2017, 9:09:19 am
Moment in moduleB februari 8e 2017, 9:09:19 am
Moment in moduleA februari 8e 2017, 9:09:19 am
Moment in moduleB februari 8e 2017, 9:09:19 am
Changing timezone in moduleB  America/New_York locale en
Moment in moduleA February 8th 2017, 3:09:19 am
Moment in moduleB February 8th 2017, 3:09:19 am
Moment in moduleA February 8th 2017, 3:09:19 am
@icambron
Copy link
Member

icambron commented Feb 7, 2017

What environment is this?

@jontelm
Copy link
Author

jontelm commented Feb 7, 2017

Node 4.7.3, ubuntu 14.04

@jontelm jontelm changed the title Is moment.tz.setDefault(); global and shared by all modules? moment.tz.setDefault() is global and shared by all modules. Feb 8, 2017
@jontelm
Copy link
Author

jontelm commented Feb 8, 2017

I think we should add a warning about this in the docs and not recommend developers not to use this function in systems like node.
http://momentjs.com/timezone/docs/#/using-timezones/default-timezone/

Something like this:
https://github.com/mashpie/i18n-node#example-usage-in-global-scope

@alburthoffman
Copy link
Contributor

moment.tz.setDefault() is obviously a global config method for module moment.tz

Not sure if it's correct that require in node only import module moment.timezone only once instead of several times. If it's true, then the example output is expected.

@jontelm
Copy link
Author

jontelm commented Feb 13, 2017

@alburthoffman Yes it's the expected output but I think a lot of people can get problems with this in node environments when a module far down in the dependency list can do something with setDefault and it will have a impact on all modules that have require('moment-timezone'). I spent a lot of time before I got this right.

If you guys agree can we add a info-text like this: https://github.com/mashpie/i18n-node#example-usage-in-global-scope but we can also just close this issue when it's not a bug.

@adamreisnz
Copy link

adamreisnz commented Oct 18, 2018

It would be great if we could have something like this:

const momentFoo = require('moment-timezone').instance();
momentFoo.tz.setDefault('foo');

const momentBar = require('moment-timezone').instance();
momentBar.tz.setDefault('bar');

momentFoo.tz() === momentBar.tz() //false

And for each moment instance to have its own local config.

This would also be great for applications using Express, where you can then create a moment instance for the duration of the request, configure it with a global timezone, and rest assured that all the rest of your middleware will use the correct timezone.

Right now, we have to set the correct timezone literally in 100+ places in our codebase, because we can't use moment.tz.setDefault().

Thoughts?

@isair
Copy link

isair commented Dec 11, 2018

You could do

import moment from 'moment-timezone';
export default moment().tz('foo');
import momentFoo from '../somewhere/moment-foo';

@jackielii
Copy link

@isair it doesn't quite work:

Uncaught TypeError: Object(...) is not a function

@marwahaha
Copy link
Member

Sure, we'll take a documentation PR at https://github.com/moment/momentjs.com

@adamreisnz - please file a ticket for your feature request in https://github.com/moment/moment-timezone

@adamreisnz
Copy link

How exactly does #684 fixes this issue? It's just a readme update to clarify the current behaviour as far as I can see.

@appsparkler
Copy link

appsparkler commented Aug 19, 2020

How about this?

const estMoment = (...args) => moment(...args).tz('America/New_York')

console.log(estMoment().toDate())
console.log(moment().toDate())

Instead of setting default; we can have a moment for various timezones (For ex. estMoment, mstMoment, pstMoment, etc) and use that everywhere. For ex estMoment('2020-08-12'); instead of moment('2020-08-12');

Good Luck...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants