Format DateTime in Text Box in Rails

前端 未结 7 1816
生来不讨喜
生来不讨喜 2021-01-31 18:59

Is there an easy way to format the display of a DateTime value in a Rails view?

For example, if I\'m doing something like:

<%= text_field         


        
7条回答
  •  有刺的猬
    2021-01-31 19:43

    In my application_helper.rb I put

    def nice_date_form(the_date)
       return the_date.strftime('%d-%m-%Y')
    end
    

    In any edit.html.erb view file with a jQuery datepicker I then use:

    <% if @activity.end_date? %> 
        <%= f.text_field :end_date, :value => nice_date_form(@activity.end_date) %>
    <% else %>
        <%= f.text_field :end_date %>
    <% end %>
    

提交回复
热议问题