Parse a time string in a specific timezone
Dealing with timezones & string parsing can be a pain. Leveraging ActiveSupport makes things a bit more predictable. See if you can spot the difference.
require "active_support/all"
value = "2016-10-01"
Time.parse(value).utc
# 2016-10-01 06:00:00 UTC
ActiveSupport::TimeZone["UTC"].parse(value).to_time.utc
# 2016-10-01 00:00:00 UTC
This example was run on a machine using Mountain Time.
Written by Nathan Hopkins
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#