LoginSignup
8
6

More than 3 years have passed since last update.

moment.jsを使ったUTCと日本時間の相互変換

Posted at

moment.jsで簡単に時間を扱えるのでUTCと日本時間(JTC)の相互の変換ができないか調べた際のメモ。
前提として、以下の全てのパターンでlocaleが日本になっていること。

日本時間の取得

これは簡単。

moment.jsで日本時間取得
moment("2019-02-21 09:00").format();  //"2019-02-21T09:00:00+09:00"

UTCの取得

これも調べてみると簡単。

moment.jsでUTC取得
moment.utc("2019-02-21 09:00").format();  //"2019-02-21T09:00:00Z"

日本時間からUTCへの変換

まずは日本時間からUTCへの変換から。

moment.jsで日本時間からUTCへの変換
moment("2019-02-21 09:00").utc().format();  //"2019-02-21T00:00:00Z"

UTCから日本時間への変換

調べるのに時間がかかってしまいやっと見つけたのが以下の方法。

moment.jsでUTCから日本時間への変換
moment.utc("2019-02-21 09:00").local().format();  //"2019-02-21T18:00:00+09:00"

moment.jsを使うと時間の操作が簡単になるのでおススメ。

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6