Rails How to Calculate Time Difference with an Integer from 3 Dates

How to compare 3 dates, (Time.now - @ model.updated_at) <: integer (converted to days). So, first this is an expression between two dates, and the difference between two dates like (amount of time) is compared (<less) with: integer.days (amount of time). Hope this makes sense ... I'm looking at different options, but I'm just getting more confused. Thanks for any help.

"#{Time.now}".to_i.days - "#{:updated_at}".to_i.days < "#{:integer}".to_i.days
+4
source share
3 answers

, Gem. , , , , , ​​.

start_time = Time.new(2013,1)
end_time = Time.new(2014,1)

TimeDifference.between(start_time, end_time).in_days
=> 365.0

,

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}
+2

Rails, ,

time1 - time2

:

diff = 3.days.ago - Time.current
# -259200.000132

to_i, .

diff.to_i
-259200

. , :

((5.days.ago - 1.day.ago).to_i / 86400).abs
# => 4
+2

gem time_diff

github

,

Time.diff(Time.parse('2011-03-06'), Time.parse('2011-03-07'))
# => {:year => 0, :month => 0, :week => 0, :day => 1, :hour => 0, :minute => 0, :second => 0, :diff => '1 day and 00:00:00'}

_

0

Source: https://habr.com/ru/post/1649294/


All Articles