Rails: convert UTC DateTime to another time zone

后端 未结 6 1437
小鲜肉
小鲜肉 2020-12-22 15:39

In Ruby/Rails, how do I convert a UTC DateTime to another time zone?

6条回答
  •  萌比男神i
    2020-12-22 16:23

    time.in_time_zone(time_zone)
    

    Example:

    zone = ActiveSupport::TimeZone.new("Central Time (US & Canada)")
    Time.now.in_time_zone(zone)
    

    or just

    Time.now.in_time_zone("Central Time (US & Canada)")
    

    You can find the names of the ActiveSupport time zones by doing:

    ActiveSupport::TimeZone.all.map(&:name)
    # or for just US
    ActiveSupport::TimeZone.us_zones.map(&:name)
    

提交回复
热议问题