Reputation: 185
I am collecting a user's time zone during signup and storing in database with the following form (relevant part).
<%= a.input :time_zone, collection: ActiveSupport::TimeZone.us_zones.map { |zone| [zone.name, zone.name] }, required: true, label: "Select your time zone" %>
However, I would like to default the selection to the user's local time zone (based on OS) to speed up registration process. I tried using
<%= a.input :time_zone, collection: ActiveSupport::TimeZone.us_zones.map { |zone| [zone.name, zone.name] }, selected: Time.now.zone, required: true, label: "Select your time zone" %>
but it doesn't work because Time.now.zone is "Eastern Daylight Time" and the zone name is "Eastern Time (US & Canada)". Is there anyway to convert the Time.now.zone to the zone name .
Upvotes: 0
Views: 313
Reputation: 3339
I got a solution here
Say, you want to set timezone America/New_York
to Time.now
(here Time.now.zone is BDT)
time = Time.now
offset = ActiveSupport::TimeZone.new('America/New_York').utc_offset()
time = time.in_time_zone("America/New_York")
time += -offset
So thank j-mcnally.
Upvotes: 1