Rails - Get the time difference in hours, minutes and seconds
You can try with this:
def time_diff(start_time, end_time)
seconds_diff = (start_time - end_time).to_i.abs
hours = seconds_diff / 3600
seconds_diff -= hours * 3600
minutes = seconds_diff / 60
seconds_diff -= minutes * 60
seconds = seconds_diff
"#{hours.to_s.rjust(2, '0')}:#{minutes.to_s.rjust(2, '0')}:#{seconds.to_s.rjust(2, '0')}"
# or, as hagello suggested in the comments:
# '%02d:%02d:%02d' % [hours, minutes, seconds]
end
And use it:
time_diff(Time.now, Time.now-2.days-3.hours-4.minutes-5.seconds)
# => "51:04:04"
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
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
Get time difference in minutes in rails the most effective way
This is not a problem, Ruby handles timezones internally.
time1 = DateTime.parse('2000-01-01 10:00:00 UTC')
time2 = DateTime.parse('2000-01-01 10:00:00 -0100')
time2 - time1
# => (1/24)
I.e. Ruby already knows that the difference between the same time in UTC and in -0100 is one hour. The time difference operator is already doing the right thing, if you let it :)
However, DateTime
is Ruby, Time
is C - the former has a lot nicer API and works awesomely with databases, but the latter is faster.
EDIT: Here's an example using Time
, still same:
time1 = Time.new(2000, 1, 1, 10, 0, 0, "+00:00")
time2 = Time.new(2000, 1, 1, 10, 0, 0, "-01:00")
time2 - time1
# => 3600.0
EDIT: Year 2999 2000
calculate difference in time in days, hours and minutes
I've used time_diff to achieve this sort of thing easily before, you may want to check it out.
rails How to calculate time difference with integer between 3 dates
I have also faced the same scenario in one of my projects and there i used the Time Difference Gem . It is very simple and easy to integrate and provides you additional helper methods to get the difference in year, month, week, day, hour, minute, and seconds.
Set the start and end date
start_time = Time.new(2013,1)
end_time = Time.new(2014,1)
Now call the calculate the difference as
TimeDifference.between(start_time, end_time).in_days
=> 365.0
Additionally it provides you liberty to fetch all differences at the same time
TimeDifference.between(start_time, end_time).in_each_component
=> {:years=>1.0, :months=>12.0, :weeks=>52.14, :days=>365.0, :hours=>8760.0, :minutes=>525600.0, :seconds=>31536000.0}
Time difference in hours
((date_2 - date_1) / 3600).round
or
((date_2 - date_1) / 1.hour).round
Related Topics
How to Add a New Action to the Existing Controller
Undefined Instance Method "Respond_To" in Rails 5 API Controller
Why Pg::Uniqueviolation: Error: Duplicate Key Value Violates Unique Constraint
How to Track Down a Memory Leak in My Ruby Code
Creating Routes with an Optional Path Prefix
Best Ruby Idiom for "Nil or Zero"
When to Use a Lambda in Ruby on Rails
Where in the Ruby Language Is %Q, %W, etc., Defined
Including a Ruby Class from a Separate File
How to Create an Array in Ruby with Default Values
How to or Should I Find an Object by the Object_Id Attribute in Ruby