date_select
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (0)
- 2.0.3 (38)
- 2.1.0 (4)
- 2.2.1 (34)
- 2.3.8 (13)
- 3.0.0 (-1)
- 3.0.9 (-3)
- 3.1.0 (1)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (21)
- 4.1.8 (13)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (13)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (6)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (5)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
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_two_digit_numbers - Set to true if you want to display two digit month and day numbers (e.g. “02” instead of “February” and “08” instead of “8”).
-
: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.
-
:month_format_string - Set to a format string. The string gets passed keys :number (integer) and :name (string). A format string would be something like “%{name} (%<number>02d)” for example. See Kernel.sprintf for documentation on format sequences.
-
:date_separator - Specifies a string to separate the date fields. Default is “” (i.e. nothing).
-
:time_separator - Specifies a string to separate the time fields. Default is “” (i.e. nothing).
-
:datetime_separator- Specifies a string to separate the date and time fields. Default is “” (i.e. nothing).
-
:start_year - Set the start year for the year select. Default is Date.today.year - 5 if you are creating new record. While editing existing record, :start_year defaults to the current selected year minus 5.
-
:end_year - Set the end year for the year select. Default is Date.today.year + 5 if you are creating new record. While editing existing record, :end_year defaults to the current selected year plus 5.
-
:year_format - Set format of years for year select. Lambda should be passed.
-
: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.
-
:selected - Set a date that overrides the actual value.
-
: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.
-
:with_css_classes - Set to true or a hash of strings. Use true if you want to assign generic styles for select tags. This automatically set classes ‘year’, ‘month’, ‘day’, ‘hour’, ‘minute’ and ‘second’. A hash of strings for :year, :month, :day, :hour, :minute, :second will extend the select type with the given value. Use html_options to modify every select tag in the set.
-
:use_hidden - Set to true if you only want to generate hidden input tags.
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.
# Generates a date select that when POSTed is stored in the article variable, in the written_on attribute. date_select("article", "written_on") # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute, # with the year in the year drop down box starting at 1995. date_select("article", "written_on", start_year: 1995) # Generates a date select that when POSTed is stored in the article 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("article", "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 article variable, in the written_on attribute, # with two digit numbers used for months and days. date_select("article", "written_on", use_two_digit_numbers: true) # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute # with the fields ordered as day, month, year rather than month, day, year. date_select("article", "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 article variable, in the written_on attribute # which is initially set to the date 3 days from the current date date_select("article", "written_on", default: 3.days.from_now) # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute # which is set in the form with today's date, regardless of the value in the Active Record object. date_select("article", "written_on", selected: Date.today) # 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("article", "written_on", prompt: { day: 'Select day', month: 'Select month', year: 'Select year' }) # Generates a date select with custom year format. date_select("article", "written_on", year_format: ->(year) { "Heisei #{year - 1988}" })
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.
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
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)
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)' )
end_year
date_select supports end_year, too.
date_select("user", "birthday", :start_year => 1940, :end_year => Date.current.year - 13)
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“)”
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
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.
Incorrectly named option
The :add_month_number option should be :add_month_numbers
rails date_select examples
I have combined some of the tips here: http://www.rubyplus.net/2016/11/rails-dateselect-examples.html
Locale
To change default locale by the parameter you can set :locale option, like below:
select_date 'user', 'birth', :locale => 'de'
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
: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.