Rails: How to parse date-time string into a specific time zone

前端 未结 7 1225
天命终不由人
天命终不由人 2020-12-01 10:23

I\'m using Rails 3.2 and ruby 1.9.3 on Debian. I have an app that collects a date, time, and timezone in the form of strings via an HTML form. Something like this:



        
7条回答
  •  眼角桃花
    2020-12-01 10:50

    Use String#in_time_zone (Rails 4+)

    I personally prefer using String#in_time_zone:

    >> '22.09.1986 10:30'.in_time_zone('Central Time (US & Canada)')
    # => Mon, 22 Sep 1986 10:30:00 CDT -05:00
    

    This parses the date and time in the String into the time zone provided.

提交回复
热议问题