开发者

Ruby on Rails: formatting date inside a field

开发者 https://www.devze.com 2023-01-07 15:47 出处:网络
My field: <%= f.text_fiel开发者_如何学Cd :expires_at, :label => false, :class => \"input-field\" %>

My field:

<%= f.text_fiel开发者_如何学Cd :expires_at, :label => false, :class => "input-field" %>

but I want the date to be kinda like this when the page loads: June, 1st, 1752 9:54:00 pm

How would I do that?


Why are you using a text_field for a datetime? Consider using time_select instead.

If you really want to format a date that way though, just use strftime.

So, in your case, add :value => @object.expires_at.strftime('%B %d, %Y %H:%M:%S %p')


You can format dates and times using the strftime method.


See Ruby's strftime and then use :value => @date_value


If you want this date format to be used throughout your application, you can set the default format in your environment.rb file:

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:default => "%a %m/%d/%Y %I:%M%p")

If you do this, every time you display a date, it will be formatted according to the date format string you've provided.

0

精彩评论

暂无评论...
验证码 换一张
取 消