In my Rails view, I have the below code that displays a datetime.
<%= link_to timeslot.opening, [@place, timeslot] %>
The result of this line is below:
2013-02-02 01:00:00 UTC
How do I change this so it displays as:
2/2/13: X:00 PST
Ruby | DateTime parse() function DateTime#parse() : parse() is a DateTime class method which parses the given representation of date and time, and creates a DateTime object. Return: given representation of date and time, and creates a DateTime object.
Use ruby's strftime()
on dates/datetimes:
<%= link_to timeslot.opening.strftime("%Y %m %d"), [@place, timeslot] %>
Have a look at the documentation to find out how the formatting works.
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] %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With