- I have a UTC date of
Tue, 16 Feb 2010 03:12:02 UTC +00:00
, for example. - I want to add 168 hours to this date to get a future UTC date.
- What's the best way to do that?
views:
100answers:
3
+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
2010-02-16 04:07:51
+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
2010-02-16 04:29:12
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
2010-02-16 04:33:34
unless of course it's the special 9 day per week calendar, where there's 18 and two thirds hours per day :)
Straff
2010-02-17 20:40:54
+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
2010-02-16 10:13:49