Module ActionView::Helpers::DateHelper
In: vendor/actionpack/lib/action_view/helpers/date_helper.rb

The Date Helper primarily creates select/option tags for different kinds of dates and date elements. All of the select-type methods share a number of common options that are as follows:

  • :prefix - overwrites the default prefix of "date" used for the select names. So specifying "birthday" would give birthday[month] instead of date[month] if passed to the select_month method.
  • :include_blank - set to true if it should be possible to set an empty date.
  • :discard_type - set to true if you want to discard the type part of the select name. If set to true, the select_month method would use simply "date" (which can be overwritten using :prefix) instead of "date[month]".

Methods

Constants

DEFAULT_PREFIX = "date" unless const_defined?("DEFAULT_PREFIX")

Public Instance methods

Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute (identified by method) on an object assigned to the template (identified by object). It’s possible to tailor the selects through the options hash, which both accepts all the keys that each of the individual select builders does (like :use_month_numbers for select_month) and a range of discard options. The discard options are :discard_year, :discard_month and :discard_day. Set to true, they’ll drop the respective select. Discarding the month select will also automatically discard the day select. It’s also possible to explicitly set the order of the tags using the :order option with an array of symbols :year, :month and :day in the desired order. Symbols may be omitted and the respective select is not included.

NOTE: Discarded selects will default to 1. So if no month select is available, January will be assumed.

Examples:

  date_select("post", "written_on")
  date_select("post", "written_on", :start_year => 1995)
  date_select("post", "written_on", :start_year => 1995, :use_month_numbers => true,
                                    :discard_day => true, :include_blank => true)
  date_select("post", "written_on", :order => [:day, :month, :year])
  date_select("user", "birthday",   :order => [:month, :day])

The selects are prepared for multi-parameter assignment to an Active Record object.

[Source]

    # File vendor/actionpack/lib/action_view/helpers/date_helper.rb, line 57
57:       def date_select(object, method, options = {})
58:         InstanceTag.new(object, method, self).to_date_select_tag(options)
59:       end

Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a specified datetime-based attribute (identified by method) on an object assigned to the template (identified by object). Examples:

  datetime_select("post", "written_on")
  datetime_select("post", "written_on", :start_year => 1995)

The selects are prepared for multi-parameter assignment to an Active Record object.

[Source]

    # File vendor/actionpack/lib/action_view/helpers/date_helper.rb, line 68
68:       def datetime_select(object, method, options = {})
69:         InstanceTag.new(object, method, self).to_datetime_select_tag(options)
70:       end

Reports the approximate distance in time between to Time objects. For example, if the distance is 47 minutes, it’ll return "about 1 hour". See the source for the complete wording list.

[Source]

    # File vendor/actionpack/lib/action_view/helpers/date_helper.rb, line 18
18:       def distance_of_time_in_words(from_time, to_time)
19:         distance_in_minutes = ((to_time - from_time) / 60).round
20: 
21:         case distance_in_minutes
22:           when 0          then "less than a minute"
23:           when 1          then "1 minute"
24:           when 2..45      then "#{distance_in_minutes} minutes"
25:           when 46..90     then "about 1 hour"
26:           when 90..1440   then "about #{(distance_in_minutes.to_f / 60.0).round} hours"
27:           when 1441..2880 then "1 day"
28:           else                 "#{(distance_in_minutes / 1440).round} days"
29:         end
30:       end

Like distance_of_time_in_words, but where to_time is fixed to Time.now.

[Source]

    # File vendor/actionpack/lib/action_view/helpers/date_helper.rb, line 33
33:       def distance_of_time_in_words_to_now(from_time)
34:         distance_of_time_in_words(from_time, Time.now)
35:       end

Returns a set of html select-tags (one for year, month, and day) pre-selected with the date.

[Source]

    # File vendor/actionpack/lib/action_view/helpers/date_helper.rb, line 73
73:       def select_date(date = Date.today, options = {})
74:         select_year(date, options) + select_month(date, options) + select_day(date, options)
75:       end

Returns a set of html select-tags (one for year, month, day, hour, and minute) preselected the datetime.

[Source]

    # File vendor/actionpack/lib/action_view/helpers/date_helper.rb, line 78
78:       def select_datetime(datetime = Time.now, options = {})
79:         select_year(datetime, options) + select_month(datetime, options) + select_day(datetime, options) +
80:         select_hour(datetime, options) + select_minute(datetime, options)
81:       end

Returns a select tag with options for each of the days 1 through 31 with the current day selected. The date can also be substituted for a hour number.

[Source]

     # File vendor/actionpack/lib/action_view/helpers/date_helper.rb, line 135
135:       def select_day(date, options = {})
136:         day_options = []
137: 
138:         1.upto(31) do |day|
139:           day_options << ((date.kind_of?(Fixnum) ? date : date.day) == day ?
140:             "<option selected=\"selected\">#{day}</option>\n" :
141:             "<option>#{day}</option>\n"
142:           )
143:         end
144: 
145:         select_html("day", day_options, options[:prefix], options[:include_blank], options[:discard_type])
146:       end

Returns a select tag with options for each of the hours 0 through 23 with the current hour selected. The hour can also be substituted for a hour number.

[Source]

     # File vendor/actionpack/lib/action_view/helpers/date_helper.rb, line 120
120:       def select_hour(datetime, options = {})
121:         hour_options = []
122: 
123:         0.upto(23) do |hour|
124:           hour_options << ((datetime.kind_of?(Fixnum) ? datetime : datetime.hour) == hour ?
125:             "<option selected=\"selected\">#{leading_zero_on_single_digits(hour)}</option>\n" :
126:             "<option>#{leading_zero_on_single_digits(hour)}</option>\n"
127:           )
128:         end
129: 
130:         select_html("hour", hour_options, options[:prefix], options[:include_blank], options[:discard_type])
131:       end

Returns a select tag with options for each of the minutes 0 through 59 with the current minute selected. The minute can also be substituted for a minute number.

[Source]

     # File vendor/actionpack/lib/action_view/helpers/date_helper.rb, line 105
105:       def select_minute(datetime, options = {})
106:         minute_options = []
107: 
108:         0.upto(59) do |minute|
109:           minute_options << ((datetime.kind_of?(Fixnum) ? datetime : datetime.min) == minute ?
110:             "<option selected=\"selected\">#{leading_zero_on_single_digits(minute)}</option>\n" :
111:             "<option>#{leading_zero_on_single_digits(minute)}</option>\n"
112:           )
113:         end
114: 
115:         select_html("minute", minute_options, options[:prefix], options[:include_blank], options[:discard_type])
116:       end

Returns a select tag with options for each of the months January through December with the current month selected. The month names are presented as keys (what’s shown to the user) and the month numbers (1-12) are used as values (what’s submitted to the server). It’s also possible to use month numbers for the presentation instead of names — set the :use_month_numbers key in options to true for this to happen. If you want both numbers and names, set the :add_month_numbers key in options to true. Examples:

  select_month(Date.today)                             # Will use keys like "January", "March"
  select_month(Date.today, :use_month_numbers => true) # Will use keys like "1", "3"
  select_month(Date.today, :add_month_numbers => true) # Will use keys like "1 - January", "3 - March"

[Source]

     # File vendor/actionpack/lib/action_view/helpers/date_helper.rb, line 157
157:       def select_month(date, options = {})
158:         month_options = []
159: 
160:         1.upto(12) do |month_number|
161:           month_name = if options[:use_month_numbers]
162:             month_number
163:           elsif options[:add_month_numbers]
164:             month_number.to_s + " - " + Date::MONTHNAMES[month_number]
165:           else
166:             Date::MONTHNAMES[month_number]
167:           end
168: 
169:           month_options << ((date.kind_of?(Fixnum) ? date : date.month) == month_number ?
170:             %(<option value="#{month_number}" selected="selected">#{month_name}</option>\n) :
171:             %(<option value="#{month_number}">#{month_name}</option>\n)
172:           )
173:         end
174: 
175:         select_html("month", month_options, options[:prefix], options[:include_blank], options[:discard_type])
176:       end

Returns a select tag with options for each of the seconds 0 through 59 with the current second selected. The second can also be substituted for a second number.

[Source]

     # File vendor/actionpack/lib/action_view/helpers/date_helper.rb, line 90
 90:       def select_second(datetime, options = {})
 91:         second_options = []
 92: 
 93:         0.upto(59) do |second|
 94:           second_options << ((datetime.kind_of?(Fixnum) ? datetime : datetime.sec) == second ?
 95:             "<option selected=\"selected\">#{leading_zero_on_single_digits(second)}</option>\n" :
 96:             "<option>#{leading_zero_on_single_digits(second)}</option>\n"
 97:           )
 98:         end
 99: 
100:         select_html("second", second_options, options[:prefix], options[:include_blank], options[:discard_type])
101:       end

Returns a set of html select-tags (one for hour and minute)

[Source]

    # File vendor/actionpack/lib/action_view/helpers/date_helper.rb, line 84
84:       def select_time(datetime = Time.now, options = {})
85:         h = select_hour(datetime, options) + select_minute(datetime, options) + (options[:include_seconds] ? select_second(datetime, options) : '')
86:       end

Returns a select tag with options for each of the five years on each side of the current, which is selected. The five year radius can be changed using the :start_year and :end_year keys in the options. The date can also be substituted for a year given as a number. Example:

  select_year(Date.today, :start_year => 1992, :end_year => 2007)

[Source]

     # File vendor/actionpack/lib/action_view/helpers/date_helper.rb, line 183
183:       def select_year(date, options = {})
184:         year_options = []
185:         y = date.kind_of?(Fixnum) ? (y = (date == 0) ? Date.today.year : date) : date.year
186:         default_start_year, default_end_year = y-5, y+5
187: 
188:         (options[:start_year] || default_start_year).upto(options[:end_year] || default_end_year) do |year|
189:           year_options << ((date.kind_of?(Fixnum) ? date : date.year) == year ?
190:             "<option selected=\"selected\">#{year}</option>\n" :
191:             "<option>#{year}</option>\n"
192:           )
193:         end
194: 
195:         select_html("year", year_options, options[:prefix], options[:include_blank], options[:discard_type])
196:       end

[Validate]