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

后端 未结 8 1771
庸人自扰
庸人自扰 2020-12-23 15:37

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:

8条回答
  •  悲哀的现实
    2020-12-23 16:36

    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]
    

提交回复
热议问题