Time.zone.now.to_date 是否等同于 Date.today? [英] Is Time.zone.now.to_date equivalent to Date.today?

查看:46
本文介绍了Time.zone.now.to_date 是否等同于 Date.today?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Time.zone.now.to_date 是否等同于 Date.today?

另一种说法:Time.zone.now.to_date == Date.today 总是 true 吗?

Another way to put it: will Time.zone.now.to_date == Date.today always be true?

如果不是,那么在应用程序时区中获取对应于now"的 Date 对象的最佳方法是什么?

If not, what's the best way to get a Date object corresponding to "now" in the application time zone?

推荐答案

它们并不总是相同的.Time.zone.now.to_date 将使用应用程序时区,而 Date.today 将使用服务器时区.因此,如果两者位于不同的日期,那么它们将是不同的.我的控制台的一个例子:

They are not always the same. Time.zone.now.to_date will use the applications time zone, while Date.today will use the servers time zone. So if the two lie on different dates then they will be different. An example from my console:

ruby-1.9.2-p290 :036 > Time.zone = "Sydney"
 => "Sydney" 
ruby-1.9.2-p290 :037 > Time.zone.now.to_date
 => Wed, 21 Sep 2011 
ruby-1.9.2-p290 :038 > Date.today
 => Tue, 20 Sep 2011 

这篇关于Time.zone.now.to_date 是否等同于 Date.today?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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