Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format datetime values to more friendly formats?

I'm fairly new to Ruby on Rails. I created a blog using SQLite3, because I am unable to install MySQL, and would like to display the date of creation next to the title.

The date of creation is created using a timestamp. As I am new to RoR I created the blog using generate scaffold so I'm not 100% sure how that all works.

At the moment, when I display the created_at field, I'm given an ugly format:

2011-12-05 14:11:10 UTC

Is there a way to change this so that it display DD-MM-YYYY HH:MM, or preferably to say "posted 30 days ago". I realize the latter would be a lot more tricky.

like image 390
howardrocks Avatar asked Dec 05 '11 15:12

howardrocks


1 Answers

I think you're looking for strftime - ruby documentation here.

Example:

irb(main):001:0> a = Time.now
irb(main):002:0> a.strftime("%d-%m-%Y %H:%M")
=> "05-12-2011 15:08"
like image 90
Colin Bradley Avatar answered Sep 28 '22 06:09

Colin Bradley