Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time difference in hours

Tags:

time

ruby

I am trying to get the difference in hours for two different Time instances. I get these values from the DB as a :datetime column

How can I do this so that it includes the months and years as well in the calculation while ignoring or rounding the minutes? Can this only be done manually or is there a function to do this?

like image 550
stellard Avatar asked Apr 09 '09 23:04

stellard


People also ask

How do you calculate difference in hours?

Calculate the duration between two times First, identify the starting and an ending time. The goal is to subtract the starting time from the ending time under the correct conditions. If the times are not already in 24-hour time, convert them to 24-hour time. AM hours are the same in both 12-hour and 24-hour time.

How do you calculate difference in hours and minutes?

To get the time difference in a single time unit (hours ,minutes or seconds), you can perform the following calculations. Total minutes between two times: To calculate the minutes between two times, multiply the time difference by 1440, which is the number of minutes in one day (24 hours * 60 minutes = 1440).

How do we calculate time?

To solve for time use the formula for time, t = d/s which means time equals distance divided by speed.

How many hours are in between two time zones?

U.S. Time Zone Map As a general rule of thumb, a change of 15° of longitude should result in a time difference of 1 hour. In the U.S., there are a total of 9 time zones used.


1 Answers

((date_2 - date_1) / 3600).round 

or

((date_2 - date_1) / 1.hour).round 
like image 135
Rômulo Ceccon Avatar answered Oct 14 '22 04:10

Rômulo Ceccon