Format DateTime in Text Box in Rails

前端 未结 7 1828
生来不讨喜
生来不讨喜 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

    Some great suggestions here, however in those cases where you really do want your text field date formatted a specific way (say you're integrating with a javascript date picker), and you're dealing with nils, I think simply making a minor tweak to your existing method would do the trick:

    :value => (@object.start_date.blank? ? '' : @object.start_date.strftime('%m/%d/%Y'))
    

提交回复
热议问题