1
1

More than 5 years have passed since last update.

Railsのタイムゾーンを日本時間に変更

Last updated at Posted at 2019-03-13

Activesupport::TimeZone

Railsが日時を扱うときは、自動的にActivesupport::TimeZoneクラスを用い、タイムゾーンはデフォルトでUTC(世界協定時刻)である

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

日本で利用するアプリケーションならば、
config.time_zone = ‘Asia/Tokyo’を設定しておくと便利なため
タイムゾーンをデフォルトのUTCから日本時間に変更してみる

config/application.rb

module SampleApp
  class Application < Rails::Application
    #Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.2
    config.time_zone = 'Asia/Tokyo'
  end
end

コンソールを立ち上げてTime.zone.nowまたは Time.currentを実行して
JST(日本標準時間)が表示されているか確認する

[1] pry(main)> Time.zone.now
=> Wed, 13 Mar 2019 01:19:55 JST +09:00


[2] pry(main)> Time.current
=> Wed, 13 Mar 2019 01:19:55 JST +09:00

最後に、画面上の時刻もJSTで表示されているのか確認する

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