Lance Pollard
Lance Pollard

Reputation: 79308

Ruby - Manipulating Time/DateTime by the Hour/Day?

Where can I find some examples on how to manipulate the time objects by days/hours/etc?

I would like to do this:

time.now_by_hour #=> "Tue Jun 15 23 MST 2010"
time.now_by_day #=> ""Tue Jun 15 MST 2010"
time.now_by_hour - 4.weeks - 3.days #=> "Sat May 15 MST 2010"

What is the recommended order of operations? The reason for this is I would like to run through lists of times and sort them by date to the hour, not to the minute and second.

Upvotes: 1

Views: 2475

Answers (3)

mxk
mxk

Reputation: 43524

Install Active Support:

$gem install activesupport

then in your Ruby script, do

require 'active_support/core_ext

That allows you to do things like these:

Time.now + 1.year
3.seconds.ago

etc.

Upvotes: 0

ceth
ceth

Reputation: 45295

Time.strftime (docs) ?

Upvotes: 1

Eimantas
Eimantas

Reputation: 49364

You chould check out active_support gem for this. It has nifty time/date manipulation methods so you can do stuff like this:

Time.now - 3.weeks - 2.days - 5.hours - 35.minutes

Upvotes: 4

Related Questions