moment-timezone icon indicating copy to clipboard operation
moment-timezone copied to clipboard

Behaviour of moment().local() after moment.tz.setDefault()

Open joelmukuthu opened this issue 10 years ago • 3 comments

I think this is a bug in moment.js, but before I post there I wanted to get your opinion on it.

After setting the default timezone, subsequent calls to moment() create moment objects in that timezone, whereas the behaviour before that is that moment() creates objects in whichever local timezone. Shouldn't it then follow that after setting a default timezone, subsequent calls to moment().local() should convert an object to the default timezone and NOT the local timezone?

Currently, this happens:

moment.tz.setDefault('America/Los_Angeles');
moment().tz(); // 'America/Los_Angeles'
moment().local().tz(); // 'America/Los_Angeles'
moment.utc().local().tz(); // undefined. Also, this converts the moment to the local timezone and not 'America/Los_Angeles'

joelmukuthu avatar Oct 12 '15 09:10 joelmukuthu

Currently, the expected behavior is that local() should always set back to the local time zone, not necessarily to the "default" time zone. See #247.

There is a bug here though. I would expect moment().local().tz() to always return undefined. It would appear that local() doesn't do anything unless the moment is in UTC.

See setOffsetToLocal line 149.

If indeed "local" is defined as "local to the machine", and not "the default time zone which happens to be local when not explicitly set", then calling .local() should always clear any time zone. This would also address #239.

@timrwood - thoughts?

mattjohnsonpint avatar Oct 12 '15 19:10 mattjohnsonpint

Just a cool little addition to this - I think it's basically the same issue.

moment.tz('2016-05-03T22:15:01+02:00', 'Europe/Berlin').local().format()
"2016-05-03T15:15:01-05:00"

moment.tz('2016-05-03T22:15:01+02:00', 'Europe/Berlin').local().add(6, 'months').format()
"2016-11-03T15:15:01+01:00"

Timezone appears to clear, but the minute you mutate the moment and UpdateOffset runs, back to the original timezone.

maggiepint avatar May 04 '16 03:05 maggiepint

I believe this will be addressed if moment/moment-rfcs#1 is adopted.

mattjohnsonpint avatar May 10 '16 06:05 mattjohnsonpint

How would one get back to the "default" timezone from a utc one? if we cannot use

moment.utc(...).local()

Do we have a ??

moment.utc(...).default()

WORMSS avatar Oct 14 '19 14:10 WORMSS