Img source: timeanddate.com

It is hard to think about a project where you do not have dates and timestamps involved in it. You may need to find the day when a user has performed a particular action, or when an event has occurred and many other cases. TimeDifference is a really helpful gem that makes it really easy to calculate the difference between two time periods. It allows you to calculate the difference in years, months, weeks, days, hours, minutes and seconds.

One of the best parts of it is the ability to also get a human friendly result that you can immediately understand without having to do any calculations:

You can install it as any other Ruby gems: Add the following line to your Gemfile:

gem ‘time_difference’

Then run bundle install. That’s it.

Here are a few examples from the README of the gem on Github:

If you want to learn more about it and view its source code, you can visit its Github page. I hope you find this hopeful.