Handles time differences and calculations.
Add this line to your application's Gemfile:
gem 'moments'
And then execute:
$ bundle
Or install it yourself as:
$ gem install moments
require 'moments'
t1 = Time.new 2015, 1, 1, 0, 0, 0
t2 = Time.now # 2020, 8, 6, 19, 29, 6
diff = Moments.difference t1, t2
puts diff.to_hash
# { years: 5, months: 7, days: 5, hours: 19, minutes: 29, seconds: 6 }
puts diff.in_months
# 67 (5 * 12 + 7)
# there are also methods for each component
from = Time.new(2022, 8, 4, 15, 52, 25, '-06:00')
to = Time.new(2022, 8, 18, 15, 52, 31, '-06:00')
difference = Moments.difference(from, to, :precise).in_minutes
# returns: 20_160.1
# supports: in_weeks, in_days, in_hours, in_minutes
ago = Moments.ago t1
puts ago.to_hash
# { years: 5, months: 7, days: 5, hours: 19, minutes: 29, seconds: 6 }
puts ago.humanized
# 5 years, 7 months, 5 days, 19 hours, 29 minutes and 6 seconds
# #humanized takes care of signularity and omits 0 values
puts Moments.difference(t1, Time.new(2015, 2, 1, 19, 0, 1)).humanized
# 1 month, 19 hours and 1 second
- Fork it ( https://github.com/excpt/moments/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request