如何将UTC时间转换为rails 4中配置的时区



我从服务器收到一个时间字符串,我想将该字符串解析或转换为配置的时区(在我的情况下是东京时区):

  • Input: "2016-05-27T09:00:00.0000000"
  • Expected output: Fri, 27 May 2016 18:00:00 JST +09:00

I try to type like this:

Time.zone.parse("2016-05-27T09:00:00.0000000")
,但返回意外输出:
Fri, 27 May 2016 09:00:00 JST +09:00

If the parsed datetime is a UTC time, add the UTC timezone explicitly to it before parsing:

# this parses the time as local time:    
Time.zone.parse("2016-05-27T09:00:00.0000000")
# => Fri, 27 May 2016 09:00:00 JST +09:00
# this parses the time as UTC and converts to local time:    
Time.zone.parse("2016-05-27T09:00:00.0000000Z")
# => Fri, 27 May 2016 18:00:00 JST +09:00

请注意日期时间字符串后面附加的"Z",这意味着这是UTC时区中的日期时间。

最新更新