rails中的Date.today和Date.current区别
source link: http://blog.ilibrary.me/2020/08/25/rails%E4%B8%AD%E7%9A%84date-today%E5%92%8Cdate-current%E5%8C%BA%E5%88%AB
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Date.today是UTC时间, Date.current是locale时间, Time.current也是locale时间, 1.days.after也是locale时间。所以不要用Date.today, 用另外3种形式. 也不要用Time.now.
在console里面执行$ Date.current
, 可以看到如下代码:
def current
::Time.zone ? ::Time.zone.today : ::Date.today
end
Date.current会判断时区,然后计算好时区以后返回当地时间。
在console里面执行$ 1.days.later
, 可以看到如下代码:
def since(time = ::Time.current)
sum(1, time)
end
这个也是基于时区来计算的,大部分情况下我们需要的就是基于时区的时间。
注: 美元符查看源码$ Date.current
是pry
的功能, 需要安装gem pry
之后才能正常使用。
大部分情况下我们应该用Date.current
, 1.days.after
和Time.current
,不要用Date.today
, 也不要用Time.now
.
Rails里面没有Date.now
.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK