我正在尝试计算 Ruby 中两个日期之间耗时(以十分之一个月为单位)。因此,例如,从2015年1月1日到2015年2月14日的时间将为1.5个月。
我发现了这个:
((date2.to_time - date1.to_time)/1.month.second).round(1)
这很好,但假设每月有 30 天(1.month==30.days
)。但显然并非所有月份都是 30 天。我正在寻找一个可以解释不同月份长度的公式。
最佳答案
如果我理解得很好,你正在寻找这样的东西:
require 'date'
def last_day_of_month(date)
(((date - date.day + 1) >> 1) - 1).day
end
def diff_in_months(date1, date2, precision=1)
if date2 < date1
return -diff_in_months(date2, date1, precision)
end
days_month_1 = last_day_of_month(date1)
ratio1 = (days_month_1 - date1.day + 1) / days_month_1.to_f
days_month_2 = last_day_of_month(date2)
ratio2 = date2.day / days_month_2.to_f
months_diff = (date2.year - date1.year) * 12 +
date2.month - date1.month - 1
(ratio1 + ratio2 + months_diff).round(precision)
end
date1 = Date.new(2015, 1, 1)
date2 = Date.new(2015, 2, 14)
date3 = Date.new(2015, 3, 14)
date4 = Date.new(2015, 4, 1)
date5 = Date.new(2016, 1, 1)
date6 = Date.new(2016, 2, 14)
diff_in_months(date1, date2) # 1.5
diff_in_months(date2, date1, 2) # -1.5
diff_in_months(date1, date3, 2) # 2.45
diff_in_months(date4, date4, 2) # 0.03 (1 day in 30)
diff_in_months(date5, date6, 2) # 1.48 (Feb'16 has 29 days)
关于ruby - 计算 ruby 中十分之一月的时间距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31948515/