如何从DateTime值中删除区域?

时间:2022-10-14 00:13:47

I have this DateTime:

我有这个DateTime:

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

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

如何删除红宝石中的区域(-0500)?我只想要这样的东西:

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

3 个解决方案

#1


26  

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

时间总是有一个区域(没有一个没有意义)。您可以选择在使用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.

有关用于构造特定格式的奥术代码,请参阅Time#strftime。

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

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

#2


1  

In addition to the accepted answer you can also add the same strftime parameters to DATE_FORMATS a Rails hash allowing you to standardise output formats in your application.

除了接受的答案,您还可以将相同的strftime参数添加到DATE_FORMATS Rails哈希,以便您在应用程序中标准化输出格式。

In config/initializers/datetime_formats.rb:

在config / initializers / datetime_formats.rb中:

Time::DATE_FORMATS[:nozone] = '%a, %d %b %Y %H:%M:%S'

Then in your code you could do:

然后在你的代码中你可以做到:

Time.zone.now.to_s(:nozone)

You could even make it the default:

您甚至可以将其设为默认值:

Time::DATE_FORMATS[:default] = '%a, %d %b %Y %H:%M:%S'
Time.zone.now.to_s

There is also a separate hash for dates:

日期还有一个单独的哈希:

Date::DATE_FORMATS[:default] = '%a, %d %b %Y'

This feature has been around for years but appears to be little known.

这个功能已存在多年,但似乎鲜为人知。

#3


0  

When all else fails

当一切都失败了

zoned_time = Time.now
unzoned_time = Time.new(zoned_time.strftime("%Y").to_i,zoned_time.strftime("%m").to_i,zoned_time.strftime("%d").to_i,zoned_time.strftime("%H").to_i,zoned_time.strftime("%M").to_i,zoned_time.strftime("%S").to_i,"+00:00")

#1


26  

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

时间总是有一个区域(没有一个没有意义)。您可以选择在使用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.

有关用于构造特定格式的奥术代码,请参阅Time#strftime。

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

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

#2


1  

In addition to the accepted answer you can also add the same strftime parameters to DATE_FORMATS a Rails hash allowing you to standardise output formats in your application.

除了接受的答案,您还可以将相同的strftime参数添加到DATE_FORMATS Rails哈希,以便您在应用程序中标准化输出格式。

In config/initializers/datetime_formats.rb:

在config / initializers / datetime_formats.rb中:

Time::DATE_FORMATS[:nozone] = '%a, %d %b %Y %H:%M:%S'

Then in your code you could do:

然后在你的代码中你可以做到:

Time.zone.now.to_s(:nozone)

You could even make it the default:

您甚至可以将其设为默认值:

Time::DATE_FORMATS[:default] = '%a, %d %b %Y %H:%M:%S'
Time.zone.now.to_s

There is also a separate hash for dates:

日期还有一个单独的哈希:

Date::DATE_FORMATS[:default] = '%a, %d %b %Y'

This feature has been around for years but appears to be little known.

这个功能已存在多年,但似乎鲜为人知。

#3


0  

When all else fails

当一切都失败了

zoned_time = Time.now
unzoned_time = Time.new(zoned_time.strftime("%Y").to_i,zoned_time.strftime("%m").to_i,zoned_time.strftime("%d").to_i,zoned_time.strftime("%H").to_i,zoned_time.strftime("%M").to_i,zoned_time.strftime("%S").to_i,"+00:00")