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

前端 未结 7 1511
既然无缘
既然无缘 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:51

    In irb:

    >>d = DateTime.now
    => #
    >> "#{d.hour.to_i - d.zone.to_i}:#{d.min}:#{d.sec}"
    => "11:16:41"
    

    will convert the time to the utc. But as posted if it is just Time you can use:

    Time.now.utc
    

    and get it straight away.

提交回复
热议问题