Reputation: 2908
I have a simple question here. I have an instance variable with a created_at. How would I convert it to Month, day, year ~ September, 13, 1987
When I tried = @example.created_at In my view it gives me 1987-09-13
Oddly enough when I do this method in console I get Sun, 13 Sep 1987
Upvotes: 7
Views: 11288
Reputation: 1711
A good practice to handle displaying dates and times is to set up internationalization for your application, more info here:
http://guides.rubyonrails.org/i18n.html
An example for date (en.yml):
en:
date:
formats:
default: "%Y-%m-%d"
short: "%b %d"
long: "%B %d, %Y"
You can display various date formats in your views like so:
l Date.current, format: :short
The advantage of this approach is that you can easily change the way all dates are displayed in your application, since the formats are defined in one place, and your application is prepared for more locales in the future.
Upvotes: 2
Reputation: 8003
You can change how a date is displayed by specifying a format like
@example.created_at.strftime("%B, %d, %Y")
# => "September, 20, 2013"
Check out various format options here
The display format is different in console and view as the default formats are different.
Upvotes: 11
Reputation: 118261
Yes..possible!
require 'date'
d = Date.parse('September, 13, 1987')
month, day, year=d.month,d.day,d.year
month # => 9
day # => 13
year # => 1987
Upvotes: 1