How do I Convert DateTime.now to UTC in Ruby?

前端 未结 7 1530
既然无缘
既然无缘 2020-12-01 04:24

If I have d = DateTime.now, how do I convert \'d\' into UTC (with the appropriate date)?

7条回答
  •  不知归路
    2020-12-01 04:35

    You can set an ENV if you want your Time.now and DateTime.now to respond in UTC time.

    require 'date'
    Time.now #=> 2015-11-30 11:37:14 -0800
    DateTime.now.to_s #=> "2015-11-30T11:37:25-08:00"
    ENV['TZ'] = 'UTC'
    Time.now #=> 2015-11-30 19:37:38 +0000
    DateTime.now.to_s #=> "2015-11-30T19:37:36+00:00"
    

提交回复
热议问题