LoginSignup
8
13

More than 5 years have passed since last update.

Railsのタイムゾーンを日本時間にする

Posted at

はじめに

RailsではデフォルトのタイムゾーンはUTC(協定時)となっています。
そのため、created_atメソッドupdated_atメソッドを使って時間を表示させてみると、
現日本時刻の9時間前になっており2019-01-13 11:28:49 UTCといったようなUTCの表記があるかと思います。

この表記を、日本時間に合わせたいと思います。

手順

config/application.rbファイルの、Applicationクラスにのなかにタイムゾーンの設定を追加する。
設定はconfig.time_zone = 'Tokyo'

config/application.rb
require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module App
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
  end
end
config/application.rb
module App
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
    config.time_zone = 'Tokyo'
  end
end

上記の設定が完了したら、一度アプリケーションを落とし再起動させます。

以上で時間表示が、2019-01-13 20:28:49 +0900といったような+0900が表示されれば完了です。

8
13
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
13