获取日期范围的十进制月份数

发布于 2024-12-01 20:34:37 字数 1215 浏览 0 评论 0原文

我尝试获取日期范围的十进制月份数。示例:

ruby-1.9.2-p0 > from = Date.new(2011, 7, 6)
 => Wed, 06 Jul 2011 
ruby-1.9.2-p0 > to = Date.new(2011, 8, 31)
 => Wed, 31 Aug 2011 
ruby-1.9.2-p0 > to - from
 => (56/1) 

所以差异是 56 天。但我想要并且需要几个月的时间: 1.83

我创建了以下代码片段,它返回正确的结果,但感觉不像 ruby​​ 方式:

months = Hash.new
(from..to).each do |date|
  unless months.key? date.beginning_of_month
    months[date.beginning_of_month] = 1
  else
    months[date.beginning_of_month] += 1
  end
end

multiplicator = 0.0
months.each do |month, days|
  multiplicator += days.to_f/month.end_of_month.day
end

return multiplicator.floor_to(2)

说实话:它看起来很丑陋而且效率很低。但我就是想不出任何更简单的方法。 你能帮我找到更好的解决方案吗?

如有更多问题,请随时问我。

非常感谢!


更新/解决方案:使用以下代码解决了问题:

months = 0.0

months += ((date_to < date_from.end_of_month ? date_to : date_from.end_of_month) - date_from + 1) / Time.days_in_month(date_from.month)
unless date_to.month == date_from.month
  months += (date_to - date_to.beginning_of_month + 1) / Time.days_in_month(date_to.month)
  months += date_to.month - date_from.month - 1
end

return months.floor_to(2)

I try to get a decimal amount of months for a date range. Example:

ruby-1.9.2-p0 > from = Date.new(2011, 7, 6)
 => Wed, 06 Jul 2011 
ruby-1.9.2-p0 > to = Date.new(2011, 8, 31)
 => Wed, 31 Aug 2011 
ruby-1.9.2-p0 > to - from
 => (56/1) 

So the difference is 56 days. But I want and need the amount of months: 1.83

I have created the following piece of code which returns the correct result but doesn't feel like the ruby way:

months = Hash.new
(from..to).each do |date|
  unless months.key? date.beginning_of_month
    months[date.beginning_of_month] = 1
  else
    months[date.beginning_of_month] += 1
  end
end

multiplicator = 0.0
months.each do |month, days|
  multiplicator += days.to_f/month.end_of_month.day
end

return multiplicator.floor_to(2)

To be honest: It looks ugly and really inefficient. But I just cannot figure out any easier way.
Can you help me to find a better solution?

For further questions feel free to ask me.

Many thanks in advance!


Update/Solution: Solved the problem with the following piece of code:

months = 0.0

months += ((date_to < date_from.end_of_month ? date_to : date_from.end_of_month) - date_from + 1) / Time.days_in_month(date_from.month)
unless date_to.month == date_from.month
  months += (date_to - date_to.beginning_of_month + 1) / Time.days_in_month(date_to.month)
  months += date_to.month - date_from.month - 1
end

return months.floor_to(2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我乃一代侩神 2024-12-08 20:34:37

更好的方法是对

  • 从开始的剩余天数/从结束的天数到
  • 结束的天数/从
  • 结束到结束的月数进行总和(从,到排除)

这样你就不会有迭代要做

a better way to do would be summation of

  • number of days left in from / number of days in from
  • number of days completed in to / number of days in to
  • number of months between from and to (from, to excluded)

This way you wont have iterations to do

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文