How do I change the zone offset for a time in Ruby on Rails?

前端 未结 13 2346
感情败类
感情败类 2020-12-30 22:03

I have a variable foo that contains a time, lets say 4pm today, but the zone offset is wrong, i.e. it is in the wrong time zone. How do I change the time zone?

When

13条回答
  •  说谎
    说谎 (楼主)
    2020-12-30 22:11

    When Parsing a Time

    I'd be interested to hear how you're setting the variable foo to begin with.

    If you're parsing a time string that doesn't have a time zone (what I was doing during a data import) then you can use String#in_time_zone to force the time zone during the parsing:

    "Fri Jun 26 2019 07:00:00".in_time_zone( "Eastern Time (US & Canada)" ) 
    # => Wed, 26 Jun 2019 07:00:00 EDT -04:00
    

    Works like a charm and is super clean.

提交回复
热议问题