Sha256: f110565bacfb0b998d9c51d76cb71db44abd1a1a77c06f9c214c15e589c0d833

Contents?: true

Size: 1.71 KB

Versions: 8

Compression:

Stored size: 1.71 KB

Contents

require 'date'

class Date


  # Return date in a sql format: YYYY-MM-DD
  #
  # ==Example
  #   d=Date.today
  #   d.to_sql => "2007-12-31"

  def to_sql
    return to_time.strftime("%Y-%m-%d")
  end


  # Return true if the date is a weekday: Mon, Tue, Wed, Thu, Fri
  #
  # ==Example
  #   d = Date.parse('2008-01-01')
  #   d.wday => 2
  #   d.weekday? => true
  
  def weekday?
    wday>0 and wday<6
  end


  # Return true if the date is a weekend: Sat, Sun
  #
  # ==Example
  #   d = Date.parse('2008-01-05')
  #   d.wday => 6
  #   d.weekend? => true

  def weekend?
    wday==0 or wday==6
  end


  # Return a random date between min & max
  #
  # ==Example
  #   d1= Date.parse('2008-01-01')
  #   d2= Date.parse('2009-01-01')
  #   Date.between(d1,d3) => Date 2008-11-22

  def self.between(min,max)
    min+rand(max-min)
  end


  # Return the age in years for a given date.
  #
  # ==Example
  #
  #   birthdate=Date.new(1980,10,31)
  #   birthdate.age_years => 28 (where 28 is the correct age for today)
  #
  # ==Example of custom dates
  #
  #   birthdate=Date.new(1980,10,31)
  #
  #   valentines = Date.new(2008,02,14)
  #   birthdate.age_years(valentines) => 27  # before the birthday
  #
  #   halloween = Date.new(2008,10,31)
  #   birthdate.age_years(halloween) => 28   # on the birthday
  #
  #   new_years_eve = Date.new(2008,12,31)
  #   birthdate.age_years(new_years_eve) => 28  # after the birthday

  def age_years(compare_date=Date.today)
    age=compare_date.year-year
    age-=1 if compare_date.month < month or (compare_date.month==month and compare_date.day < day)
    age
  end


  # Return the age in days for a given date.

  def age_days(compare_to_date=Date.today)
    (compare_to_date-self).to_i
  end


end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
webget_ramp-1.7.1.8 lib/webget_ramp/date.rb
webget_ramp-1.7.1.7 lib/webget_ramp/date.rb
webget_ramp-1.7.1.6 lib/webget_ramp/date.rb
webget_ramp-1.7.1.5 lib/webget_ramp/date.rb
webget_ramp-1.7.1.4 lib/webget_ramp/date.rb
webget_ramp-1.7.1.3 lib/webget_ramp/date.rb
webget_ramp-1.7.1.2 lib/webget_ramp/date.rb
webget_ramp-1.7.1.1 lib/webget_ramp/date.rb