rails datetime_select year month day hours minutes

Ruby rails date_select

Instance Public methods

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

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.
  • :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 setdiscard_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.

Example: with day more one day, the hours begin at 10 hours.

<%= f.datetime_select :start_datetime , :selected => Date.today + 1 , start_hour: 10, :minute_step => 10 %>

rails datetime_select year month day hours minutes
rails datetime_select year month day hours minutes

 

This will give you an hour range between 5pm – 10pm

<%= f.datetime_select :time,  minute_step: 30, start_hour: 17, end_hour: 22 %>

Other options,

<%= f.datetime_select :activated_at %>

# =>
# <div class="field">
# <select id="user_activated_at_1i" name="user[activated_at(1i)]">
#   <option value="2015" selected="selected">2015</option>
#   <option value="2016">2016</option>
#     ... year
# </select>
# <select id="user_activated_at_2i" name="user[activated_at(2i)]">
#   <option value="1" selected="selected">January</option>
#   <option value="2">February</option>
#     ... month
#   <option value="12">December</option>
# </select>
# <select id="user_activated_at_3i" name="user[activated_at(3i)]">
#   <option value="1">1</option>
#   <option value="2">2</option>
#     ... day
#   <option value="31">31</option>
# </select>
#  —
# <select id="user_activated_at_4i" name="user[activated_at(4i)]">
#   <option value="00">00</option>
#   <option value="01">01</option>
#     ... hours
#   <option value="23">23</option>
# </select>
#  :
#  <select id="user_activated_at_5i" name="user[activated_at(5i)]">
#   <option value="00">00</option>
#   <option value="01">01</option>
#     ... minutes
#   <option value="59">59</option>
# </select>


# params is year, month, day, hours and minutes.
{
  "user" => {
    "name" => "Username",
    "activated_at(1i)" => "2017",
    "activated_at(2i)" => "10",
    "activated_at(3i)" => "24",
    "activated_at(4i)" => "10",
    "activated_at(5i)" => "40"
  },
  "commit"=>"Create User"
}

Trả lời

Thư điện tử của bạn sẽ không được hiện thị công khai. Các trường bắt buộc được đánh dấu *

Bạn có thể sử dụng các thẻ HTML và thuộc tính sau: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>