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

前端 未结 7 1426
既然无缘
既然无缘 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
    => #<DateTime: 11783702280454271/4800000000,5/12,2299161>
    >> "#{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.

    0 讨论(0)
提交回复
热议问题