Rails: convert UTC DateTime to another time zone

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

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

6条回答
  •  情歌与酒
    2020-12-22 16:20

    In plain ruby, with only require 'date', use the new_offset method:

    require 'date'
    
    d=DateTime.parse('2000-01-01 12:00 +0200')
    l=d.new_offset('-0700')
    u=l.new_offset('UTC')
    puts "#{u.strftime('%a %F %T %Z')} ❖ #{l.strftime('%a %F %T %Z')}"
    

    Tested with ruby 2.3.7 that came standard on Mac OS X 10.13.

提交回复
热议问题