How do I format datetime in rails?

前端 未结 4 1504
无人共我
无人共我 2021-02-05 08:51

In my Rails view, I have the below code that displays a datetime.

<%= link_to timeslot.opening, [@place, timeslot] %> 

The result of this

4条回答
  •  -上瘾入骨i
    2021-02-05 09:50

    You should use a helper for this.

    If you want to convert from UTC to PST you can use the in_time_zone method

    def convert_time(datetime)
      time = Time.parse(datetime).in_time_zone("Pacific Time (US & Canada)")
      time.strftime("%-d/%-m/%y: %H:%M %Z")
    end
    
    <%= link_to convert_time(timeslot.opening), [@place, timeslot] %>
    

提交回复
热议问题