Ruby: Calculate time difference between 2 times
I don't understand why you think it should return 4 hours or why it does return 22 hours. 20 hours would be correct for your example:
require 'time'
a = Time.parse('2015-12-31 22:00:00 +0100')
b = Time.parse('2015-12-31 02:00:00 +0100')
a - b
#=> 72000.0 # difference in seconds
(a - b) / 3600
#=> 20.0 # difference in hours
Update: It seems like you are dealing only with the time portion and not with the actual date. And I assume the maximum difference you will have to deal with is 24 hours:
def time_difference(time_a, time_b)
difference = time_b - time_a
if difference > 0
difference
else
24 * 3600 + difference
end
end
a = Time.parse('2015-12-31 22:00:00 +0100')
b = Time.parse('2015-12-31 02:00:00 +0100')
time_difference(a, b) / 3600
# => 4 # hours
a = Time.parse('2015-12-31 02:00:00 +0100')
b = Time.parse('2015-12-31 22:00:00 +0100')
time_difference(a, b) / 3600
# => 20 # hours
Calculating difference in time between two Time objects
This should work:
hours = ((created - updated) / 1.hour).round
Related question: Rails Time difference in hours
Difference in hours between 2 DateTime
First of all, from the docs:
So when should you use
DateTime
in Ruby and when should you useTime
? Almost certainly you’ll want to useTime
since your app is probably dealing with current dates and times. However, if you need to deal with dates and times in a historical context you’ll want to useDateTime
...
If you want to continue using DateTime
, its subtraction method gives you the difference in fractions of a day
DateTime.new(2000, 1, 3) - DateTime.new(2000, 1, 1)
# (2/1)
Getting that into hours is actually kind of impossible because there aren't always 24 hours per day. You need to convert to Time
objects to get the difference in seconds, then convert from seconds to hours
diff_in_hours = (dt1.to_time - dt2.to_time) / 3_600
Number of days between two Time instances
Difference of two times is in seconds. Divide it by number of seconds in 24 hours.
(t1 - t2).to_i / (24 * 60 * 60)
How to find the difference between two time in ruby?
If you want it in seconds, you can just convert both to time stamps, and then subtract
time_difference_in_sec = (DateTime.now.to_time.to_i - @given_time.to_time.to_i).abs
Else you wind up dealing with rational numbers, and the like as seen in the other answers..
Get difference between 2 hours
I'm not sure I'm getting completely the point, but maybe this could work for you:
Given the times, without converting to string.
my_time = '2019-02-24 16:52'
a = Time.parse(my_time)
b = Time.utc(Time.now.year, Time.now.month, Time.now.day, Time.now.hour, Time.now.min)
With Ruby 2.6.1 (Object#then):
[ ( Time.parse(a.to_s.split[1]) - Time.parse(b.to_s.split[1]) ) / 3600 ].then { |delta| delta[0] < 0 ? 24 + delta[0] : delta[0] }
With Ruby 2.5 use Object#yield_self.
Time difference in hours
((date_2 - date_1) / 3600).round
or
((date_2 - date_1) / 1.hour).round
how to get time difference in minutes in rails for two date time fields?
As created_at
and updated_at
are of type DateTime
, this should work.
created = article.created_at
updated = article.updated_at
minutes = ((updated - created) * 24 * 60).to_i
Explanation:
Subtracting two DateTimes returns the elapsed time in days. In the below example e-d
will return (28807336643183/28800000000000)
. So to convert this into minutes, we need to multiply it by 24*60
(As the day has 24 hours and each hour has 60 minutes)
Example(tested):
d = DateTime.now
=> Tue, 13 Jun 2017 10:21:59 +0530
2.3.3 :003 > e = DateTime.now + 1.day
=> Wed, 14 Jun 2017 10:22:21 +0530
g = ((e-d) * 24 * 60).to_i
=> 1440
Related Topics
Import CSV in Batches of Lines in Rails
Should Repeat a Number of Times
When Should I Use Datetime VS Date, Time Fields in Ruby/Rails
Ruby Code for Modifying Outer Quotes on Strings
Iterate Through Array of Hashes in Ruby
Working with Multiple Processes in Ruby
Adapters Not Working with Datamapper
Regex to Match Mm/Dd/Yyyy Hh:Mm:Ss Am or Pm
How to Use String Methods on Utf-8 Characters
Return Values Were Not Extracted in Ruby
Ruby on Rails - Pagination on Search Result
Why Do We Need Nginx with Thin on Production Setup
Empty Strings at the Beginning and End of Split
How to Reinstall Ruby with Readline Support
Algorithm to Shuffle an Array Randomly Based on Different Weights
Rails Not Working for New Project. Showingerror " Superclass Mismatch for Class Cipher (Typeerror)"