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

railsアプリのデフォルトのタイムゾーンを変更する

Posted at

問題

公開したアプリのDate.todayが、開発環境ではローカルの時刻を取得できたが、公開したらUTC時刻になっていた。

やったこと

デフォルトのタイムゾーンを日本時間に変更

(アプリ名)/config/application.rb
config.time_zone = 'Asia/Tokyo'

これによって、データベース上ではUTC時刻で登録されているが、アプリ内ではTokyoの時刻として表示する(UTC+9:00されたのが確認できると思う)。

本日の日付けを取得するメソッドとして
Date.today→Time.zone.todayに変更

データベース上に、デフォルトではUTC時刻で登録されて、Time.zoneを使ってローカルの日時に変更することが多いので、このやり方を覚えておく。

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