Search code examples
moment-timezone

How to clear the timezone associated with a moment-timezone object


Using moment-timezone, if you have a moment object set with a timezone e.g. moment.tz('America/New_York'), how can you clear the timezone and change the moment to "local" time?

Notably, moment.local() does not work (a subsequent call to moment.tz() still equals America/New_York). I can confirm that my local time is not America/New_York.

The only solution I have found is to set an invalid timezone, e.g. moment.tz('local'). This logs a warning to the console that you have attempted to set an invalid timezone (annoying), but it does clear the timezone associated with the moment object, returning it to a local moment object. The warning that moment-timezone spits out emphasizes that this solution may break in the future though, and it's obviously a hack.

What's the proper way to accomplish this?


Solution

  • Update

    This issue was caused by a bug in moment-timezone. As of version, 0.5.24, moment.local() should correctly clear the time zone associated with a moment-timezone object.

    Original

    At the...moment! This appears to be a bug in moment-timezone (see #738, #258). The current work-around I've found is to first use moment.utc() (which clears the timezone and also sets the moment in "utc" mode) and then moment.local(). I.e. moment.utc().local().