Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

format a date in the rails model

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.

like image 910
Omnipresent Avatar asked Feb 25 '23 07:02

Omnipresent


1 Answers

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.

like image 171
coreyward Avatar answered Feb 26 '23 19:02

coreyward