Convert DateTime String to UTC in rails

后端 未结 3 1413
逝去的感伤
逝去的感伤 2021-01-31 15:29

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

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-31 15:44

    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.

提交回复
热议问题