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
Comments
What environment is this? |
Node 4.7.3, ubuntu 14.04 |
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. Something like this: |
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. |
@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. |
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 Thoughts? |
You could do
|
@isair it doesn't quite work:
|
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 |
How exactly does #684 fixes this issue? It's just a readme update to clarify the current behaviour as far as I can see. |
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 Good Luck... |
Default timezone is shared between all modules:
server.js
moduleA.js
moduleB.js
Output after 3 seconds:
The text was updated successfully, but these errors were encountered: