datetime_select(object_name, method, options = {}) public

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.

Show source
Register or log in to add new notes.
January 14, 2009
2 thanks

See #data_select for available options

Available symbols for “options” hash are described on date_select page

August 3, 2009
0 thanks

Separating date and time

Option :datetime_separator can be set too. Default is ‘ — ’

September 2, 2010
0 thanks

Set hour and/or minutes

To set the hour and/or the minutes, you can use:

<%= f.datetime_select(:offer_end, :default => { :hour => 23, :minute => 59 }) %>
December 2, 2011 - (>= v3.0.0)
0 thanks

Difference in DateTimes ...

Mostly, the database date columns are DateTime instances. Whereas Rails adds several useful utility functions to DateTime (in activesupport), there is some confusing behavior. This is at least somewhat surprising.

Two subtract two DateTime instances, you should always “round” the returned value. Suppose:

d1 = DateTime.now d2 = d1 - 5.days.ago

then, d1 - d2 is a Rational, which tells you the difference between dates only after rounding. Thus, diff_in_days = (d1 - d2).round