views:

100

answers:

3
  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?
+9  A: 

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
Doug Neiner
+1  A: 

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
marocchino
OK, I have to laugh. `9.days` might be simpler, but that would make `216` hours. I think you mean `7.days` or `1.week` :)
Doug Neiner
unless of course it's the special 9 day per week calendar, where there's 18 and two thirds hours per day :)
Straff
+1  A: 

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.

Shadowfirebird