Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with datetime in Rails

How to properly extract a time only from the datetime object (post.created_at, to be precise) with/without the influence of TimeZone? How to extract the day, month and year values? How to extract the day/month/year via a custom pattern (is it possible)?

like image 679
gmile Avatar asked Aug 11 '09 11:08

gmile


1 Answers

Time in String format:

post.created_at.strftime("FORMAT STRING HERE")
# Without the influence of time-zone: I take it that you want UTC
post.created_at.utc.strftime("FORMAT STRING HERE")

Link for the strftime documentation: http://ruby-doc.org/core/classes/Time.html#method-i-strftime

For getting the hour, minute and second values:

post.created_at.hour
post.created_at.min
post.created_at.sec
like image 122
Swanand Avatar answered Oct 16 '22 02:10

Swanand