【发布时间】:2012-04-23 09:21:08
【问题描述】:
我想要ActiveSupport::TimeWithZone 实例中的日期部分。我为此使用了to_date 函数,但它会提前一天返回日期。
例如,如果日期时间是2012-04-11 09:05:00 UTC,如果我调用to_date,那么它返回2012-04-10,但返回2012-04-11。
另外我没有使用特定的时区(默认为 UTC)
应用程序正在Ruby 1.8.7 和Rails 3.0.2 上运行
谁能告诉我为什么它给出了错误的日期?如果有更好的方法从给定的DateTime(ActiveSupport::TimeWithZone 的实例)中获取日期(Date 的实例)部分,请建议我
编辑: 有人在这里也面临同样的问题http://pastebin.com/sn8ExZiQ
注意:Time.zone 返回'UTC'
【问题讨论】:
-
Time.zone 的值是多少?
-
@FrederickCheung Time.zone 返回 UTC
-
你调用
to_date的ActiveSupport::TimeWithZone对象的zone的输出是什么? -
@shime 它返回
#<ActiveSupport::TimeZone:0xb7565fb0 @tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, @utc_offset=nil, @name="UTC", @current_period=nil>
标签: ruby ruby-on-rails-3 datetime