Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Follow up to changing Date format from Time.now to post.created_at

I previously asked a question on stack overflow about how to format my date in a readable manner. This was the gist of it was this.

How do I take

2010-06-14 19:01:00 UTC

and turn it into

June 14th, 2010

The answer that I used was calling post.date and simply adding the below to post.rb

@date = Time.now
@date.strftime("%B %d, %Y")

It does show the date in words but I don't want to show the current date. I want the post.created_at date.

How can I modify the above (or what do I need to add elsewhere) to get this to work?

like image 837
bgadoci Avatar asked Jan 20 '26 14:01

bgadoci


2 Answers

post.created_at.strftime("%B %d, %Y")

A good resource for strftime is http://strfti.me

like image 99
Geoff Lanotte Avatar answered Jan 22 '26 07:01

Geoff Lanotte


You can define your own formatters for reuse as initializers.

 # config/initializers/time_formats.rb
  Time::DATE_FORMATS[:nice] = "%B %d, %Y"

Then use

 User.created_at.to_formatted_s(:nice)

Reference

  • http://apidock.com/rails/ActiveSupport/CoreExtensions/DateTime/Conversions/to_formatted_s
like image 40
Jamie Wong Avatar answered Jan 22 '26 07:01

Jamie Wong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!