Skip to content

Instantly share code, notes, and snippets.

@bps
Created October 6, 2009 21:04
Show Gist options
  • Save bps/203413 to your computer and use it in GitHub Desktop.
Save bps/203413 to your computer and use it in GitHub Desktop.
I often want to see a delta between two dates.
#!/usr/bin/env ruby
require 'date'
raise 'Give two parseable arguments' unless ARGV.length == 2
d1 = DateTime.parse(ARGV[0])
d2 = DateTime.parse(ARGV[1])
if d1 < d2
d1, d2 = d2, d1
end
hours,minutes,seconds,frac = Date.day_fraction_to_time(d1 - d2)
puts "The difference is #{hours / 24} day#{hours / 24 == 1 ? '' : 's'}, #{hours % 24} hour#{hours % 24 == 1 ? '' : 's'}, and #{minutes} minute#{minutes == 1 ? '' : 's'}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment