In Rails, what's the nicest way to create a specific time (not now) in a particular time zone?

后端 未结 5 722
庸人自扰
庸人自扰 2021-02-01 01:36

Here\'s one way, but it\'s can you think of a nicer or more \'railsy\' method?

>> Time.use_zone(\'Sydney\'){ Time.zone.parse(\'2011-04-12 2pm\') }
=> T         


        
5条回答
  •  孤独总比滥情好
    2021-02-01 02:16

    For parsing a date within a specific time zone, you can use ActiveSupport::TimeZone

    > ActiveSupport::TimeZone["Sydney"].parse("2011-04-12 2pm")
    => Tue, 12 Apr 2011 14:00:00 EST 10:00
    

    TimeZone API documentation is here: http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html#method-c-5B-5D

提交回复
热议问题