我试图找出两个不同日期的时间差(没有天/年/月)。
例子:
#ruby >1.9
time1 = Time.now - 1.day
time2 = Time.now
#code to make changes
#test:
time1 == time2 # TRUE
我的解决方案:
time1 = time1.strftime("%H:%M").to_time
time2 = time2.strftime("%H:%M").to_time
#test
time1 == time2 #True
#passes
我想知道是否有更好的方法来做到这一点?也许我们可以让日期与时间 1/时间 2 相同?
最佳答案
我认为您实际上想要以下内容:
((time2 - time1) % (60 * 60 * 24)) == 0
关于ruby-on-rails - 比较两次而不考虑相关日期 - Ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17331216/