keruilin
keruilin

Reputation: 17512

Working with date and time

  1. I have a UTC date of Tue, 16 Feb 2010 03:12:02 UTC +00:00, for example.
  2. I want to add 168 hours to this date to get a future UTC date.
  3. What's the best way to do that?

Upvotes: 4

Views: 1618

Answers (3)

Shadowfirebird
Shadowfirebird

Reputation: 757

In vanilla Ruby it's not much more difficult:

time_string = 'Tue, 16 Feb 2010 03:12:02 UTC +00:00'
new_time = DateTime.parse( time_string ) + 7

(You could just use the Date class, it would still work.)

I admit adding in hours is a little more tricky.

Upvotes: 1

Doug Neiner
Doug Neiner

Reputation: 66191

You tagged the question rails so here is how you can do this in Rails, using some of the helpers:

time_string = 'Tue, 16 Feb 2010 03:12:02 UTC +00:00'
new_time = Time.parse( time_string ) + 168.hours

If you already have it as a Time object, just add the 168.hours:

new_time = old_time + 168.hours

Or you can just add 1.week:

new_time = old_time + 1.week

Upvotes: 9

marocchino
marocchino

Reputation: 93

FYI, '9.days' is more simpler than '168.hours'.

>> new_time = Time.parse( time_string ) + 168.hours
=> Tue Feb 23 03:12:02 UTC 2010
>> new_time = Time.parse( time_string ) + 9.days
=> Thu Feb 25 03:12:02 UTC 2010

Upvotes: 1

Related Questions