letz letz - 16 days ago 4
Ruby Question

How can I set timezone in simple form datetime field?

I have a model with a datetime field, and is stored in UTC, how can I display that date in simple form gem in a specific timezone?

Already tried with the option

input_html: {value: @model.date.in_time_zone('Eastern Time (US & Canada)')}


Note: I can't change the timezone of rails app

Answer

It will work if you set the timezone in the controller:

Example:

around_action :set_time_zone, if: :current_user

private

def set_time_zone(&block)
   Time.use_zone(current_user.time_zone, &block)
end