Getting the difference between 2 dates

I want to get the number of days, hours and minutes between two dates. How to do it?

+4
source share
2 answers
d3=d1-d2 d3.days d3.hours d3.min d3.sec #etc... 

Note: This is the answer to the original question , and not to another question that he edited after the answer.

However, since @dukedave is mentioned in the comments, this is not a real working answer. Please use @RealCasually answer below.

-5
source

I did not find the Ruby-ish seconds_to_hours function (although it is trivial to build), but at the bottom of this post there is an obvious solution.

 s = Time.now e = Time.now + 3.hours d = e - s => 10803.318176 

Note. This value is simply a float representing seconds between two date values.

I would like him to come back. To do this, you divide the time, in seconds.

 b = d / 1.hour.seconds => 3.0009217155555556 

My goal was to get the decimal value of the clock, rounded to the next 6 minutes, like this:

 r = b.round(1) => 3.0 

Summarizing:

 r = ((e - s) / 1.hour.seconds).round(1) => 3.0 
+6
source

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


All Articles