Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

ruby on rails 3 - Change date format in the view to dd/mm/yyyy

The default date format in Ruby is yyyy-mm-dd, but I needed them to be dd/mm/yyyy in the view

I have wrote a date_format file in config/initializers as:

Time::DATE_FORMATS.merge!( :uk_format => '%m/%d/%Y')

This didn't work since I need to call date.to_s(:uk_format) in order to use this format. But since I'm using <%= f.text_field :paymentDate %> to display the date, I'm not sure I can add to_s to this case.

Can someone help me out please.


Edit 1

Actually date.to_s(:uk_format) doesn't work either, how do I use initializer properly.....?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See: Change default Ruby Time format

Adapted my answer linked to above for the Date and format specified:


Since you're using Rails, take advantage of the I18n support: http://guides.rubyonrails.org/i18n.html#adding-date-time-formats

# config/locales/en.yml
en:
  date:
    formats:
      default: "%d/%m/%Y"

Then you can call I18n.l Date.today to get out a formatted date. Or in your view, you can use <%= l @foo.created_at %>.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...