ruby-on-rails - 在 DateTime 减法中返回分钟

标签 ruby-on-rails ruby datetime

以下返回 (5/12),一个 Rational 对象。

d = DateTime.current
d2 = d + 10.hours
d2 - d

我如何得到两个时间点的分钟差?

最佳答案

(d2 - d).class
=> Rational

Rational: A rational number can be represented as a paired integer number; a/b (b>0). Where a is numerator and b is denominator. Integer a equals rational a/1 mathematically.

d2 - d
=> (5/12)  #get the number of days

((d2 - d) * 24 * 60).to_i
=> 600    # get the number of minutes

关于ruby-on-rails - 在 DateTime 减法中返回分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32783438/

相关文章:

ruby-on-rails - 清理Rails中的输入XSS和HTML输入

ruby-on-rails - ActiveRecord::Base 的未定义方法#sanitize

ruby-on-rails - Ruby 不断给出未知的错误信息

python - 如何在 Python 中从毫秒创建日期时间?

php - 将来如何设置时间戳

ruby-on-rails - rails 错误 : Index name '...' on table '...' already exists while running rails db:migrate

ruby - Un-monkey 修补 Ruby 中的类/方法

Ruby soap4r wsdl2ruby.rb 错误

sql - 在 SQL Server 中删除日期时间的时间部分的最佳方法

mysql - Ruby on Rails 和 MySQL 插入和选择每周事件