Forum: Ruby on Rails How to present Rails form datetime select in different time zone?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Andrew Havens (Guest)
on 2013-08-22 10:58
(Received via mailing list)

I have a Rails application with a default time zone of UTC. This works
for almost everything. Now I have a situation where I would like to
the values in a datetime_select offset by a specific time zone. As far
as I
know, this helper does not have an option to specify the time zone that
used when persisting this data. When the form is submitted, it only
the year, month, day, hour, and minute fields. ActiveRecord is
for converting these values into the time zone configured for the
application. In this case, I want to change the time zone for only these
fields so that it is localized to the user (but still store the correct
time). How can I make this change? Thanks for your help!

Colin Law (Guest)
on 2013-08-22 11:31
(Received via mailing list)
On 22 August 2013 00:59, Andrew Havens <> wrote:
> fields so that it is localized to the user (but still store the correct UTC
> time). How can I make this change? Thanks for your help!

You could adjust the values in the controller prior to saving the data.

Andrew Havens (Guest)
on 2013-08-22 21:24
(Received via mailing list)
I found a way to do it. To convert the multi-parameter attributes that
submitted in the form to a specific time zone, add a method in your
controller to manually convert the params into a datetime object. I
to add this to the controller because I did not want to affect the model
behavior. You should still be able to set a date on the model and assume
your date was set correctly.

def create
  @model =[:my_model])
  # ...end
def update
  # ...end
def convert_datetimes_to_pdt(field)
  datetime = (1..5).collect {|num| params['my_model'].delete
"#{field}(#{num}i)" }
  if datetime[0] and datetime[1] and datetime[2] # only if a date has
been set
    params['my_model'][field] = Time.find_zone!("Pacific Time (US &

Now the datetime will be adjusted to the correct time zone. However,
the user goes to edit the time, the form fields will still display the
in UTC. To fix this, we can wrap the fields in a call toTime.use_zone:

Time.use_zone("Pacific Time (US & Canada)") do
  f.datetime_select :start_dateend
This topic is locked and can not be replied to.