[Moment Timezone] How to Use moment.tz

Tadashi Shigeoka ·  Fri, April 24, 2015

I’ll introduce how to use the moment.tz method from Moment Timezone.

Moment.js

moment.tz has two interfaces, which are clearly documented in the official documentation, so I’ll explain based on that.

There are two interfaces for using timezones with Moment.js.

moment.tz(…, String) is used to create a moment with a timezone, and moment().tz(String) is used to change the timezone on an existing moment.

A direct translation would be something like this:

Two interfaces are provided for using timezones with Moment.js:
  • moment.tz(..., String) creates a moment instance with a timezone
  • moment().tz(String) converts the timezone of an existing moment instance

Usage Example of moment.tz(..., String)

var a = moment.tz('2015-01-01 00:00:00', "America/Los_Angeles");
a.format();
// '2015-01-01T00:00:00-08:00'

a.utc().format();
// '2015-01-01T08:00:00+00:00'

Usage Example of moment().tz(String)

var b = moment.utc('2015-01-01 00:00:00').tz("America/Los_Angeles");
b.format();
// '2014-12-31T16:00:00-08:00'

b.utc().format();
// '2015-01-01T00:00:00+00:00'

If you don’t understand it well, you might end up with different behavior than expected, so make sure to understand moment.tz thoroughly before using it.

That’s all from the Gemba.