Wednesday, 15 August 2012

ruby on rails - Convert time zone of datetime returned by f.datetime_select -



ruby on rails - Convert time zone of datetime returned by f.datetime_select -

when utilize f.datetime_select in rails form, returns datetime controller in utc, e.g.

2014-06-18t11:00:00+00:00

my local time zone set melbourne (+10) specified in application.rb file:

config.time_zone = 'melbourne'

so when retrieve datatimes database automatically converted melbourne (+10) timezone, e.g.

2014-06-17 19:00:00 +1000

i want compare datetime returned f.datetime_select field in database. how can this?

i.e. how can alter time zone of datetime returned f.datetime select 'melbourne' (+10) without changing actual time? i.e. convert:

2014-06-18t11:00:00+00:00

to

2014-06-18t11:00:00+10:00

all dates stored in database in utc time. when app new date 'params' have 2 options: save activerecord model, , during ar perform heavylifting of deciding of timezone meant. if don't want save info model, have deal yourself. date select command homecoming 3 specially formatted strings in params hash. let's named field in form 'birthdate'. controller like:

"<your_model_name>" => {... , "birthdate(3i)" => "<day>", "birthdate(2i)" => "<month>", "birthdate(1i)" => "<year>", ...}

so deal info like:

time.zone.parse("#{ params[:model]['birthdate(3i)'] }-#{ params[:model]['birthdate(2i)'] }-#{ params[:model]['birthdate(1i)'] }")

and yeah know looks ugly, , after research surprised there no 'out of box' solution )

ruby-on-rails datetime

No comments:

Post a Comment