I have a date of birth in the rails model and I display it in different places. Every time I have to specifically format it in mm/dd/yyyy
format. Is there something I can do in my model so that every time I get the dob out it comes in mm/dd/yyyy
format.
You can define a quick formatted_birthday
method in your model, but if you're just outputting this to views you can use Rails' built in date formatting output:
http://guides.rubyonrails.org/i18n.html#adding-date-time-formats
# config/locales/en.yml
en:
time:
formats:
birthday: "%m/%d/%Y"
Then in your view just use:
<%= l person.birthday, :format => 'birthday' %>
Or you can change birthday
to default
in the format definition and you can omit the :format
option all together.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With