王筝的博客
ruby学习
the time difference in months can be treated as:
`((Time.current – updated_at)/1.month.seconds).to_i`
This will give you:

“`
2.4.5 :013 > ((Time.current – 1.month.ago)/1.month.seconds).to_i
=> 1
2.4.5 :014 > ((Time.current – 10.month.ago)/1.month.seconds).to_i
=> 10
2.4.5 :015 > ((Time.current – 0.month.ago)/1.month.seconds).to_i
=> 0
“`
Also there is a good gem more human style like https://github.com/tmlee/time_difference a bit old but does the job.

prefer `Time.current` because this guy decides if it wants to be a `Time.zone.now` or a `Time.now` depending on the project settings.
This is also valid for `Date.current` and `DateTime.current`
a great explanation here https://nandovieira.com/working-with-dates-on-ruby-on-rails

Leave a Reply