I have a string like this:
\"2010-01-01 12:30:00\"
I need that to convert to UTC from the current local time zone.
I tried this, but it
Time.parse("2010-01-01 12:30:00").getutc
EDIT
(grinding teeth while thinking about the nightmare which is Ruby/Rails date/time handling)
OK, how about this:
Time.zone.parse("2010-01-01 12:30:00").utc
Note that Time.zone.parse
returns a DateTime
, while appending the .utc
gives you a Time
. There are differences, so beware.
Also, Time.zone is part of Rails (ActiveSupport), not Ruby. Just so you know.
For APIs you can use:
utc_date = Time.parse("2013-05-31 00:00").utc.iso8601 #=> Result will be: 2013-05-30T21:00:00Z
You can check these articles:
In Rails 4 and above you can directly use in_time_zone
"2010-01-01 12:30:00".in_time_zone
#=> Fri, 01 Jan 2010 12:30:00 EST -05:00
"2010-01-01 12:30:00".in_time_zone.utc
#=> 2010-01-01 17:30:00 UTC