How do I get the number of seconds between two DateTimes in Ruby on Rails

Ruby on-RailsDatetime

Ruby on-Rails Problem Overview


I've got code that does time tracking for employees. It creates a counter to show the employee how long they have been clocked in for.

This is the current code:

  start_time = Time.parse(self.settings.first_clock_in)
  total_seconds = Time.now - start_time
  hours = (total_seconds/ 3600).to_i
  minutes = ((total_seconds % 3600) / 60).to_i
  seconds = ((total_seconds % 3600) % 60).to_i

This works fine. But because Time is limited to the range of 1970 - 2038 we are trying to replace all Time uses with DateTimes. I can't figure out how to get the number of seconds between two DateTimes. Subtracting them yields a Rational which I don't know how to interpret, whereas subtracting Times yields the difference in seconds.

NOTE: Since Ruby 1.9.2, the hard limit of Time is removed. However, Time is optimized for values between 1823-11-12 and 2116-02-20.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Subtracting two DateTimes returns the elapsed time in days, so you could just do:

elapsed_seconds = ((end_time - start_time) * 24 * 60 * 60).to_i

Solution 2 - Ruby on-Rails

Or, more readably:

diff = datetime_1 - datetime_2
diff * 1.days # => difference in seconds; requires Ruby on Rails

Note, what you or some other searchers might really be looking for is this:

diff = datetime_1 - datetime_2
Date.day_fraction_to_time(diff) # => [h, m, s, frac_s]

Solution 3 - Ruby on-Rails

You can convert them to floats with to_f, though this will incur the usual loss of precision associated with floats. If you're just casting to an integer for whole seconds it shouldn't be big enough to be a worry.

The results are in seconds:

>> end_time.to_f - start_time.to_f
=> 7.39954495429993

>> (end_time.to_f - start_time.to_f).to_i
=> 7

Otherwise, you could look at using to_formatted_s on the DateTime object and seeing if you can coax the output into something the Decimal class will accept, or just formatting it as plain Unix time as a string and calling to_i on that.

Solution 4 - Ruby on-Rails

Others incorrectly rely on fractions or helper functions. It's much simpler than that. DateTime itself is integer underneath. Here's the Ruby way:

stop.to_i - start.to_i

Example:

start = Time.now
 => 2016-06-21 14:55:36 -0700
stop = start + 5.seconds
 => 2016-06-21 14:55:41 -0700
stop.to_i - start.to_i
 => 5

Solution 5 - Ruby on-Rails

I am using ruby-2.1.4 and for me the following worked

Time.now - Time.new(2014,11,05,17,30,0)

gave me the time difference in seconds

reference: ruby doc

Solution 6 - Ruby on-Rails

there's a method made for that:

Time.now.minus_with_coercion(10.seconds.ago)

equals 10.

Source: http://apidock.com/rails/Time/minus_with_coercion

Hope I helped.

Solution 7 - Ruby on-Rails

Define a Ruby function like this,

def time_diff(start_time, end_time)
  seconds_diff = (start_time - end_time).to_i.abs

  days = seconds_diff / 86400 
  seconds_diff -= days * 86400

  hours = seconds_diff / 3600  
  seconds_diff -= hours * 3600

  minutes = seconds_diff / 60
  seconds_diff -= minutes * 60

  seconds = seconds_diff

  "#{days} Days #{hours} Hrs #{minutes} Min #{seconds} Sec"
 end

And Call this function,

time_diff(Time.now, Time.now-4.days-2.hours-1.minutes-53.seconds)

Solution 8 - Ruby on-Rails

why not use the built in "sec" . Here is an example :

t1 = Time.now.sec 

elapsed_t = Time.now.sec - t1

puts "it took me :  #{elapsed_t} seconds to do something that is useful \n"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTilendorView Question on Stackoverflow
Solution 1 - Ruby on-RailsDavid MaView Answer on Stackoverflow
Solution 2 - Ruby on-RailsAvram DorfmanView Answer on Stackoverflow
Solution 3 - Ruby on-RailsLukeView Answer on Stackoverflow
Solution 4 - Ruby on-RailsAmin ArianaView Answer on Stackoverflow
Solution 5 - Ruby on-RailsSom PoddarView Answer on Stackoverflow
Solution 6 - Ruby on-RailsFrancoisView Answer on Stackoverflow
Solution 7 - Ruby on-RailsramyaView Answer on Stackoverflow
Solution 8 - Ruby on-Railsz atefView Answer on Stackoverflow