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
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.
http://momentjs.com/timezone/docs/#/using-timezones/default-timezone/
Something like this:
https://github.com/mashpie/i18n-node#example-usage-in-global-scope
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 moment.tz.setDefault()
.
Thoughts?
You could do
import moment from 'moment-timezone';
export default moment().tz('foo');
import momentFoo from '../somewhere/moment-foo';
@isair it doesn't quite work:
Uncaught TypeError: Object(...) is not a function
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.
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...
Most helpful comment
How exactly does #684 fixes this issue? It's just a readme update to clarify the current behaviour as far as I can see.