Simple time helper in rails
I am building a simple event system and i am having problem with rails time helper conversions. I want to be able to have a 12hour clock that has an output of开发者_运维百科 something like 12:00am or 12:00pm using something like <%= event.start_at %>
to give the time output in the 12hour clock format
If event.start_at
is already a date or time object (e.g. an ActiveRecord instance) then you could do something like:
<%= event.start_at.to_s(:my_format) %>
You'd need to define :my_format
in an initializer:
# config/initializers/date_and_time_formats.rb
Time::DATE_FORMATS[:my_format] = '%I:%M%P' # 12:30pm
You can tweak the format to your liking and use it over and over again using to_s(:my_format)
. @Dave Newton pointed out where to go for formatting above.
http://apidock.com/ruby/Time/strftime
精彩评论