1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

nuxtのdayjs-moduleで日本のタイムゾーン表示にする

Last updated at Posted at 2021-04-07

GMTのタイムゾーンで表示されてしまう

nuxtで日付を扱うライブラリとしてdayjs-moduleを選択した
そのまま出力する分にはよいが、format('YYYY.MM.DD')で出力するとグリニッジ標準時なので、想定外の時刻になる

$dayjs('2021-03-01T19:00:00.000Z')

【出力結果】
Mon, 01 Mar 2021 19:00:00 GMT

$dayjs('2021-03-01T19:00:00.000Z').format('YYYY/MM/DD H:mm:ss')

【出力結果】
2021/03/02 04:00:00

Day.jsは自動で自国のタイムゾーンになるって書いてあったのですが、dayjs-moduleはだめなんでしょうか…
<html lang="ja">にしてみたが、結果は変わらず

 head: {
    htmlAttrs: {
      lang: 'ja'
    }
 }

nuxt.configの設定を追加する

readmeにある通り、dayjsの設定でdefaultTimeZone: 'Asia/Tokyo',を設定する
https://github.com/nuxt-community/dayjs-module#1-register-dayjs-module-to-your-nuxt-application

が、結果は変わらず

tzを使うように明示する

Day.jsでのtimezoneの設定方法を参考に、tz()をつると日本時間になった
https://day.js.org/docs/en/plugin/timezone

$dayjs.tz('2021-03-01T19:00:00.000Z')

【出力結果】
Mon, 01 Mar 2021 10:00:00 GMT

GMTなので-9時間されてる

$dayjs.tz('2021-03-01T19:00:00.000Z').format('YYYY/MM/DD H:mm:ss')

【出力結果】
2021/03/01 19:00:00

ちゃんと出た

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?