Date Distance

Tagged with Ruby dates

Language: Ruby

View as text

class Date
  def distance_to(end_date)
    years = end_date.year - year
    months = end_date.month - month
    days = end_date.day - day
    if days < 0
      days += 30
      months -= 1
    end
    if months < 0
      months += 12
      years -= 1
    end
    {:years => years, :months => months, :days => days}
  end
  
  def distance_from_now
    self.distance_to(Date.today)
  end
end

Last updated at 21:36 PM on Jul 07, 2008 by Brian Hogan

SnippetStash costs money to host and develop. The service is free for everyone to use
but if you found it useful please consider making a small donation.