在 Ruby on Rails 中查找两个日期之间的月数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9428605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find number of months between two Dates in Ruby on Rails
提问by phoenixwizard
I have two Ruby on Rails DateTime objects. How to find the number of months between them? (Keeping in mind they might belong to different years)
我有两个 Ruby on Rails DateTime 对象。如何找到它们之间的月数?(记住它们可能属于不同的年份)
回答by Massimiliano Peluso
(date2.year * 12 + date2.month) - (date1.year * 12 + date1.month)
more info at http://www.ruby-forum.com/topic/72120
回答by dgilperez
A more accurate answer would consider days in the distance.
更准确的答案是考虑远处的天数。
For example, if you consider that the month-distance from 28/4/2000and 1/5/2000is 0rather than 1, then you can use:
例如,如果您认为与28/4/2000和的月距离1/5/2000是0而不是1,那么您可以使用:
(date2.year - date1.year) * 12 + date2.month - date1.month - (date2.day >= date1.day ? 0 : 1)
回答by knotito
Give a try to
试一试
((date2.to_time - date1.to_time)/1.month.second).to_i
回答by shock_one
You can rephrase the question as "how many 1st days is there between the beginnings of months of the dates", and then use functional-style data transformations:
您可以将问题重新表述为“日期的月份开始之间有多少 1st 天”,然后使用函数式数据转换:
(date1.beginning_of_month...date2.beginning_of_month).select { |date| date.day == 1 }.size
回答by Andreykul
Assuming both are dates:
((date2 - date1).to_f / 365 * 12).roundsimple.
假设两者都是日期:
((date2 - date1).to_f / 365 * 12).round简单。
回答by Ankit Varshney
start_date = Date.today
end_date = Date.today+90
months = (end_date.month+end_date.year*12) - (start_date.month+start_date.year*12)
//months = 3
回答by U?ur Y?lmaz
def difference_in_months(date1, date2)
month_count = (date2.year == date1.year) ? (date2.month - date1.month) : (12 - date1.month + date2.month)
month_count = (date2.year == date1.year) ? (month_count + 1) : (((date2.year - date1.year - 1 ) * 12) + (month_count + 1))
month_count
end
回答by Zack
Another solution that I found (built off a solution posted here already) is for if you want the result to include fractions of a month. For example the distance is 1.2 months.
我发现的另一个解决方案(建立在此处发布的解决方案之上)适用于如果您希望结果包含一个月的一小部分。例如距离是1.2 months。
((date2.to_time - date1.to_time)/1.month.second).round(1) #Tenth of a month Ex: 1.2
((date2.to_time - date1.to_time)/1.month.second).round(2) #Hundreth, ex: 1.23 months
etc...
回答by ole
Here's a brute forcing variant:
这是一个蛮力变体:
date1 = '2016-01-05'.to_date
date2 = '2017-02-27'.to_date
months = 0
months += 1 while (date2 << (count+1)) >= date1
puts months # => 13
date2must be always greater than date1
date2必须总是大于 date1
回答by Dhanush Balachandran
Here is another method. This will help to calculate number of whole months between two dates
这是另一种方法。这将有助于计算两个日期之间的整月数
def months_difference(date_time_start, date_time_end)
curr_months = 0
while (date_time_start + curr_months.months) < date_time_end
curr_months += 1
end
curr_months -= 1 if (date_time_start + curr_months.months) > date_time_end
curr_months.negative? ? 0 : curr_months
end

