Created
August 7, 2020 22:15
-
-
Save timuruski/c8e6a710e5e2fb9dd7eaa4b9565841d3 to your computer and use it in GitHub Desktop.
Example of an algorithm to get years, months, days between two days (expressed in two ways)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "time" | |
p now = Time.parse("2020-08-07") | |
p date = Time.parse("2013-08-07") | |
delta, _ = (now.to_i - date.to_i).divmod(24 * 60 * 60) | |
years, rest = delta.divmod(365) | |
months, days = rest.divmod(30) | |
puts "years: #{years}" | |
puts "months: #{months}" | |
puts "days: #{days}" | |
# years: 7 | |
# months: 0 | |
# days: 2 | |
require "active_support/all" | |
puts "active_support" | |
delta = now - date | |
years, rest = delta.divmod(1.year) | |
months, rest = rest.divmod(1.month) | |
days, rest = rest.divmod(1.day) | |
puts "years: #{years}" | |
puts "months: #{months}" | |
puts "days: #{days}" | |
# years: 7 | |
# months: 0 | |
# days: 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment