Work with date and time

  • I have a UTC Tue, 16 Feb 2010 03:12:02 UTC +00:00 date Tue, 16 Feb 2010 03:12:02 UTC +00:00 , for example.
  • I want to add 168 hours to this date to get the future UTC date.
  • What is the best way to do this?
+4
source share
3 answers

You noted the rails question, here's how you can do it 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 a Time object, just add 168.hours :

 new_time = old_time + 168.hours 

Or you can simply add 1.week :

 new_time = old_time + 1.week 
+9
source

FYI, "9.days" is easier 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 
+1
source

In vanilla ruby, it's not much harder:

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

(You can just use the Date class, it will work anyway.)

I admit that adding to a watch is a bit trickier.

+1
source

Source: https://habr.com/ru/post/1301402/


All Articles