如何从 DateTime 值中删除区域? [英] How can I remove the zone from a DateTime value?

查看:41
本文介绍了如何从 DateTime 值中删除区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个日期时间:

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

如何删除 ruby​​ 中的区域(-0500)?我只是想要这样的东西:

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

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

推荐答案

时间总是有一个区域(没有区域就没有意义).您可以在使用 DateTime#strftime:

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

参见Time#strftime 用于构建特定格式的神秘代码.

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

或者,您可能希望 将您的 DateTime 转换为 UTC 以获得更一般的表示.

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

这篇关于如何从 DateTime 值中删除区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆