bor*_*gvo 6 ruby ruby-on-rails
说我希望明天和现在之间的区别(以小时为单位).
我尝试过的:
t = (DateTime.tomorrow - DateTime.now)
(t / 3600).to_i
=> 0
Run Code Online (Sandbox Code Playgroud)
为什么给0?
我究竟做错了什么?
shi*_*vam 16
这是因为DateTime.tomorrow没有任何时间价值.这里:
DateTime.tomorrow
# => Wed, 22 Apr 2015
Run Code Online (Sandbox Code Playgroud)
如果您通过DateTime的官方文档,您可以看到没有方法tomorrow.它基本上Date#tomorrow.
您可以使用.to_time获取默认的本地时间00:00:00
DateTime.tomorrow.to_time
# => 2015-04-22 00:00:00 +0530
(DateTime.tomorrow.to_time - DateTime.now) / 1.hours
# => 9.008116581638655
Run Code Online (Sandbox Code Playgroud)
要获得日期之间的确切小时差异:
(DateTime.tomorrow.to_time - Date.today.to_time) / 1.hours
# => 24.0
Run Code Online (Sandbox Code Playgroud)
小智 5
尝试这个
t = (DateTime.tomorrow.to_time - Date.today.to_time)
t = (t / 3600).to_i
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7626 次 |
| 最近记录: |