Convert Time from one time zone to another in Rails

前端 未结 5 728
粉色の甜心
粉色の甜心 2020-11-29 17:25

My created_at timestamps are stored in UTC:

>> Annotation.last.created_at
=> Sat, 29 Aug 2009 23:30:09 UTC +00:00

How

5条回答
  •  醉梦人生
    2020-11-29 18:06

    Use the in_time_zone method of the DateTime class

    Loading development environment (Rails 2.3.2)
    >> now = DateTime.now.utc
    => Sun, 06 Sep 2009 22:27:45 +0000
    >> now.in_time_zone('Eastern Time (US & Canada)')
    => Sun, 06 Sep 2009 18:27:45 EDT -04:00
    >> quit
    

    So for your particular example

    Annotation.last.created_at.in_time_zone('Eastern Time (US & Canada)')
    

提交回复
热议问题