How can I remove the zone from a DateTime value?

后端 未结 3 762
渐次进展
渐次进展 2021-01-03 18:22

I have this DateTime:

=> Fri, 03 Feb 2012 11:52:42 -0500

How can I remove the zone(-0500) in ruby? I just want something like this:

3条回答
  •  北海茫月
    2021-01-03 18:53

    Time always has a zone (it has no meaning without one). You can choose to ignore it when printing by using DateTime#strftime:

    now = DateTime.now
    puts now
    #=> 2012-02-03T10:01:24-07:00
    
    puts now.strftime('%a, %d %b %Y %H:%M:%S')
    #=> Fri, 03 Feb 2012 10:01:24
    

    See Time#strftime for the arcane codes used to construct a particular format.

    Alternatively, you may wish to convert your DateTime to UTC for a more general representation.

提交回复
热议问题