Created
February 5, 2009 21:02
-
-
Save jamster/59000 to your computer and use it in GitHub Desktop.
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
# EXTRACTED FROM THIS THREAD: | |
# http://rails.lighthouseapp.com/projects/8994/tickets/879-finding-the-days-weeks-months-years-between-two-dates | |
require "rubygems" | |
require "activesupport" | |
#activesupport is necessary for Date.parse (I think) | |
class Date | |
def self.days_between(start, finish) | |
(finish - start).to_i | |
end | |
def self.weeks_between(start, finish) | |
days_between(start, finish) / 7 | |
end | |
def self.months_between(start, finish) | |
if start.year != finish.year | |
difference = (12 - start.month) + (12 * years_between(start, finish)) + finish.month | |
else | |
difference = finish.month - start.month | |
end | |
difference -= 1 if finish.day < start.day | |
difference | |
end | |
def self.years_between(start, finish) | |
difference = finish.year - start.year | |
difference -= 1 if difference > 0 && (finish.month < start.month || finish.day < start.day) | |
difference | |
end | |
end | |
#=> Date.years_between(Date.parse('04/26/78', "%m/%d/%Y"), Date.today) | |
#=> 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment