date_select(object_name, method, options = {}, html_options = {}) public

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).

Options

  • :use_month_numbers - Set to true if you want to use month numbers rather than month names (e.g. “2” instead of “February”).

  • :use_short_month - Set to true if you want to use abbreviated month names instead of full month names (e.g. “Feb” instead of “February”).

  • :add_month_numbers - Set to true if you want to use both month numbers and month names (e.g. “2 - February” instead of “February”).

  • :use_month_names - Set to an array with 12 month names if you want to customize month names. Note: You can also use Rails’ i18n functionality for this.

  • :date_separator - Specifies a string to separate the date fields. Default is “” (i.e. nothing).

  • :start_year - Set the start year for the year select. Default is Time.now.year - 5.

  • :end_year - Set the end year for the year select. Default is Time.now.year + 5.

  • :discard_day - Set to true if you don’t want to show a day select. This includes the day as a hidden field instead of showing a select field. Also note that this implicitly sets the day to be the first of the given month in order to not create invalid dates like 31 February.

  • :discard_month - Set to true if you don’t want to show a month select. This includes the month as a hidden field instead of showing a select field. Also note that this implicitly sets :discard_day to true.

  • :discard_year - Set to true if you don’t want to show a year select. This includes the year as a hidden field instead of showing a select field.

  • :order - Set to an array containing :day, :month and :year to customize the order in which the select fields are shown. If you leave out any of the symbols, the respective select will not be shown (like when you set :discard_xxx => true. Defaults to the order defined in the respective locale (e.g. [:year, :month, :day] in the en locale that ships with Rails).

  • :include_blank - Include a blank option in every select field so it’s possible to set empty dates.

  • :default - Set a default date if the affected date isn’t set or is nil.

  • :disabled - Set to true if you want show the select fields as disabled.

  • :prompt - Set to true (for a generic prompt), a prompt string or a hash of prompt strings for :year, :month, :day, :hour, :minute and :second. Setting this option prepends a select option with a generic prompt (Day, Month, Year, Hour, Minute, Seconds) or the given prompt string.

If anything is passed in the html_options hash it will be applied to every select tag in the set.

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

Examples

# Generates a date select that when POSTed is stored in the post variable, in the written_on attribute
date_select("post", "written_on")

# Generates a date select that when POSTed is stored in the post variable, in the written_on attribute,
# with the year in the year drop down box starting at 1995.
date_select("post", "written_on", :start_year => 1995)

# Generates a date select that when POSTed is stored in the post variable, in the written_on attribute,
# with the year in the year drop down box starting at 1995, numbers used for months instead of words,
# and without a day select box.
date_select("post", "written_on", :start_year => 1995, :use_month_numbers => true,
                                  :discard_day => true, :include_blank => true)

# Generates a date select that when POSTed is stored in the post variable, in the written_on attribute
# with the fields ordered as day, month, year rather than month, day, year.
date_select("post", "written_on", :order => [:day, :month, :year])

# Generates a date select that when POSTed is stored in the user variable, in the birthday attribute
# lacking a year field.
date_select("user", "birthday", :order => [:month, :day])

# Generates a date select that when POSTed is stored in the user variable, in the birthday attribute
# which is initially set to the date 3 days from the current date
date_select("post", "written_on", :default => 3.days.from_now)

# Generates a date select that when POSTed is stored in the credit_card variable, in the bill_due attribute
# that will have a default day of 20.
date_select("credit_card", "bill_due", :default => { :day => 20 })

# Generates a date select with custom prompts
date_select("post", "written_on", :prompt => { :day => 'Select day', :month => 'Select month', :year => 'Select year' })

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

Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that all month choices are valid.

Show source
Register or log in to add new notes.
August 28, 2008
10 thanks

Get year to show in descending order (Today to 1920 for example)

The way people think of time start and end would be 1920 to today. This made me think “but I want it show the current year first then down.” Well it’s as simple as swapping the start_year and end_year.

date_select :date, :start_year => Date.current.year, :end_year => 1920

# => 2008, 2007, 2006, 2005 ... 1920
August 25, 2008 - (v2.1.0)
5 thanks

Disable default date

If you want a date selector that initially doesn’t have a date selected you can pass it the option :include_blank.

date_select("project", "due_date", :include_blank => true)
February 17, 2009
4 thanks

Date_select with assert_valid_keys

If you are using date_select with assert_valid_keys you have to allow 3 parameters named field(1i), field(2i) and field(3i).

For example with field

date_select("post", "written_on")

You have to allow following fields:

params[:post].assert_valid_keys( 
  'written_on(1i)', 'written_on(2i)', 'written_on(3i)'
)
August 13, 2008 - (v2.1.0)
2 thanks

end_year

date_select supports end_year, too.

date_select("user", "birthday", :start_year => 1940, :end_year => Date.current.year - 13)
February 4, 2010 - (>= v2.1.0)
1 thank

All dates in the database are stored in UTC and all dates in Ruby are in a local timezone

With the timezone support introduced in Rails 2.1 the idea is that all dates in the database are stored in UTC and all dates in Ruby are in a local timezone.

Time.zone.now == Time.now # => false

as Peter Marklund lights up this in his blog:

http://marklunds.com/articles/one/402

“They will only be converted to UTC for you if they are ActiveSupport::TimeWithZone objects, not if they are Time objects. This means that you are fine if you use Time.zone.now, 1.days.ago, or Time.parse(”2008-12-23“).utc, but not if you use Time.now or Time.parse(”2008-12-23“)”

January 29, 2010
1 thank

Passing html options (Ruby hash parameters)

When you have two default hash parameters at the end of a function call, you need to use it as the following:

options = { :include_blank => true, :default => @my_object.my_method }

date_select :my_object, :my_method, options, :class => 'my_css_class'

You can try it for yourself on this example:

def test_funct a = {}, b = {}
  puts "a: #{a.inspect}"
  puts "b: #{b.inspect}"
end

test_funct :x => 'x', :y => 'y'  # all the parameters are collected for the hash a
September 25, 2011
1 thank

Parsing the Date Params into a New Date Object.

Useful for when you need to create a Date object from these manually, such as for reporting.


If the date select input is start_date and it belongs to the report form object:

@start_date = Date.civil(params[:report]["start_date(1i)"].to_i,
                         params[:report]["start_date(2i)"].to_i,
                         params[:report]["start_date(3i)"].to_i)

@start_date.class
# => Date

@start_date
# => Sun, 25 Sep 2011  # For example.

Use a similar method for DateTime situations.

December 23, 2008
0 thanks

Incorrectly named option

The :add_month_number option should be :add_month_numbers

December 1, 2016
0 thanks
April 19, 2012 - (<= v3.2.1)
0 thanks

Locale

To change default locale by the parameter you can set :locale option, like below:

select_date 'user', 'birth', :locale => 'de'
March 1, 2013
0 thanks

Default field order

If you want to set an app wide default order for the fields (rather than passing :order each time), use the locale file.

eg. edit config/locale/en.yml to include:

en:
  date:
    order: 
      - :day
      - :month
      - :year
June 16, 2009
0 thanks

:discard_month implicitly sets :discard_day to true

This may not be the behaviour that you want, and setting :discard_day => false doesn’t change this. One way of getting around this is to hide the month field using CSS e.g.

#some_date_field_2i {
  display:none;
}

If you use the :default option for the date_select, the correct default month will be passed through to the controller. Using this with :discard_year will give you a dropdown with only the day, but preserve the month and year as provided by :default.