Parent

Date

Date

This new version of Date extension has been largely improved by porting some of the methods used by ActiveSupport. The old version already had much in common with the Active Support library, so it was decided to take it a step further in that direction for the sake of interoparability.

Hopefully most of these methods will find there way into Ruby’s own standard library eventually.

The biggest difference with ActiveSupport is the lack of many of the “English-esque” methods, and that we use # with Date::FORMAT, instead of # with Date::DATE_FORMATS. We do not override the standard # method like ActiveSupport does.

Constants

FORMAT

Public Class Methods

current() click to toggle source

Returns Time.zone.today when config.time_zone is set, otherwise just returns Date.today.

    # File lib/more/facets/date.rb, line 43
43:   def self.current
44:     ::Time.zone_default ? ::Time.zone.today : ::Date.today
45:   end
tomorrow() click to toggle source

Returns a new Date representing the date 1 day after today (i.e. tomorrow’s date).

    # File lib/more/facets/date.rb, line 38
38:   def self.tomorrow
39:     ::Date.today.tomorrow
40:   end
yesterday() click to toggle source

Returns a new Date representing the date 1 day ago (i.e. yesterday’s date).

    # File lib/more/facets/date.rb, line 33
33:   def self.yesterday
34:     ::Date.today.yesterday
35:   end

Public Instance Methods

advance(options) click to toggle source

Provides precise Date calculations for years, months, and days. The options parameter takes a hash with any of these keys: :years, :months, :weeks, :days.

     # File lib/more/facets/date.rb, line 140
140:   def advance(options)
141:     d = self
142:     d = d >> options.delete(:years) * 12 if options[:years]
143:     d = d >> options.delete(:months)     if options[:months]
144:     d = d +  options.delete(:weeks) * 7  if options[:weeks]
145:     d = d +  options.delete(:days)       if options[:days]
146:     d
147:   end
ago(seconds) click to toggle source

Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) and then subtracts the specified number of seconds

     # File lib/more/facets/date.rb, line 165
165:   def ago(seconds)
166:     to_time.since(-seconds)
167:   end
beginning_of_day() click to toggle source

Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)

     # File lib/more/facets/date.rb, line 177
177:   def beginning_of_day
178:     to_time
179:   end
Also aliased as: midnight
change(options) click to toggle source

Returns a new Date where one or more of the elements have been changed according to the options parameter.

Examples:

  Date.new(2007, 5, 12).change(:day => 1)                  # => Date.new(2007, 5, 1)
  Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12)
     # File lib/more/facets/date.rb, line 155
155:   def change(options)
156:     ::Date.new(
157:       options[:year]  || self.year,
158:       options[:month] || self.month,
159:       options[:day]   || self.day
160:     )
161:   end
days_in_month() click to toggle source

Returns the number of days in the date’s month.

  Date.new(2004,2).days_in_month #=> 28

CREDIT: Ken Kunz.

    # File lib/more/facets/date.rb, line 90
90:   def days_in_month
91:      Date.civil(year, month, 1).day
92:   end
days_of_month() click to toggle source
    # File lib/more/facets/date.rb, line 94
94:   def days_of_month
95:     (1..days_in_month).to_a
96:   end
in(seconds) click to toggle source
Alias for: since
midnight() click to toggle source
Alias for: beginning_of_day
month_name() click to toggle source

Get the month name for this date object

CREDIT: Benjamin Oakes

     # File lib/more/facets/date.rb, line 102
102:   def month_name
103:     MONTHNAMES[self.month]
104:   end
since(seconds) click to toggle source

Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) and then adds the specified number of seconds

     # File lib/more/facets/date.rb, line 171
171:   def since(seconds)
172:     to_time.since(seconds)
173:   end
Also aliased as: in
stamp(format=:default) click to toggle source

Convert to a formatted string. See DATE_FORMATS for predefined formats.

This method is aliased to to_s.

Examples:

  date = Date.new(2007, 11, 10)       # => Sat, 10 Nov 2007

  date.stamp(:db)            # => "2007-11-10"
  date.stamp(:short)         # => "10 Nov"
  date.stamp(:long)          # => "November 10, 2007"
  date.stamp(:rfc822)        # => "10 Nov 2007"

Adding your own formats to stamp

You can add your own formats to the Date::FORMAT hash. Use the format name as the hash key and a strftime string as the value. Eg.

  Date::FORMAT[:month_and_year] = "%B %Y"
     # File lib/more/facets/date.rb, line 125
125:   def stamp(format=:default)
126:     if formatter = FORMAT[format]
127:       strftime(formatter)
128:     else
129:       to_s
130:     end
131:   end
to_date() click to toggle source

A method to keep Time, Date and DateTime instances interchangeable on conversions. In this case, it simply returns self.

    # File lib/more/facets/date.rb, line 49
49:   def to_date
50:     self
51:   end
to_datetime() click to toggle source

Converts a Date instance to a DateTime, where the time is set to the beginning of the day and UTC offset is set to 0.

Example:

  date = Date.new(2007, 11, 10)  # => Sat, 10 Nov 2007

  date.to_datetime               # => Sat, 10 Nov 2007 00:00:00 0000
    # File lib/more/facets/date.rb, line 60
60:   def to_datetime
61:     ::DateTime.civil(year, month, day, 0, 0, 0, 0)
62:   end
to_time(form=:local) click to toggle source

Converts a Date instance to a Time, where the time is set to the beginning of the day. The timezone can be either :local or :utc (default :local).

  date = Date.new(2007, 11, 10)  # => Sat, 10 Nov 2007

  date.to_time                   # => Sat Nov 10 00:00:00 0800 2007
  date.to_time(:local)           # => Sat Nov 10 00:00:00 0800 2007

  date.to_time(:utc)             # => Sat Nov 10 00:00:00 UTC 2007
    # File lib/more/facets/date.rb, line 74
74:   def to_time(form=:local)
75:     ::Time.send(form, year, month, day)
76:     #::Time.send("#{form}_time", year, month, day)
77:   end
tomorrow() click to toggle source

Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time

     # File lib/more/facets/date.rb, line 188
188:   def tomorrow
189:     self + 1
190:   end
xmlschema() click to toggle source
    # File lib/more/facets/date.rb, line 80
80:   def xmlschema
81:     to_time.xmlschema
82:   end
yesterday() click to toggle source

Convenience method which returns a new Date/DateTime representing the time 1 day ago

     # File lib/more/facets/date.rb, line 183
183:   def yesterday
184:     self - 1
185:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.