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.

[Rails] タイムゾーン情報のない UTC の日付文字列をローカルのタイムゾーン (JST など) に変更する

Last updated at Posted at 2020-12-29

2020/01/01 00:00:00 という文字列がある。これを UTC の日付文字列だと解釈してローカルのタイムゾーン、例えば JST の時刻オブジェクトに変換したい。

Time.zone.name
#=> "Tokyo"

以下だと 2020/01/01 00:00:00 のままタイムゾーンが JST に変わるだけ。

'2020/01/01 00:00:00'.in_time_zone
#=> Wed, 01 Jan 2020 00:00:00 JST +09:00

Time.zone.parse('2020/01/01 00:00:00')
#=> Wed, 01 Jan 2020 00:00:00 JST +09:00

そこでこうする!

time_zone = ActiveSupport::TimeZone.new('UTC')
time_zone.parse('2020/01/01 00:00:00').in_time_zone
#=> Wed, 01 Jan 2020 09:00:00 JST +09: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?