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

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

    d = DateTime.now.utc

    Oops!

    That seems to work in Rails, but not vanilla Ruby (and of course that is what the question is asking)

    d = Time.now.utc
    

    Does work however.

    Is there any reason you need to use DateTime and not Time? Time should include everything you need:

    irb(main):016:0> Time.now
    => Thu Apr 16 12:40:44 +0100 2009
    

提交回复
热议问题