如何在两个范围内查找共同日期 [英] How Do I Find Common Dates in Two Ranges
本文介绍了如何在两个范围内查找共同日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个日期范围, start_date1..end_date1
和 start_date2..end_date2
,是否有一个简单的红宝石的方式来查找两个范围内的所有日期?
I have 2 date ranges, start_date1..end_date1
and start_date2..end_date2
, is there an easy "ruby" way to find all the dates that are in both ranges?
推荐答案
您可以使用
(start_date1..end_date1).to_set & (start_date2..end_date2).to_set
下面是一个完整的示例:
here's a fully worked example:
require 'date'
require 'set'
((Date.today - 3)..(Date.today + 2)).to_set & (Date.today..(Date.today + 5)).to_set
字符,您也可以这样做
(start_date1..end_date1).to_set & start_date2..end_date2
但我认为原始版本更清晰。
but I think the original version is clearer.
这篇关于如何在两个范围内查找共同日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文