[Moment Timezone] moment.tz の使い方

Moment Timezone の moment.tz メソッドの使い方を紹介します。

Moment.js

moment.tz には 2 つのインターフェースが用意されていて公式ドキュメントに明記されているので、それを元に説明していきます。

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.

直訳すると下記のような感じでしょうか。

Moment.js で timezone を使うために、

  • moment.tz(…, String) は moment instance を timezone 付きで作成する
  • moment().tz(String) は既存の moment instance の timezone を変換する

という 2 つインターフェースが用意されています。

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'

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'

よく理解して使わないと期待していたのと違う挙動になっているかもしれないので、よく理解して moment.tz を使うようにしましょう。