How Do I Find Common Dates in Two Ranges

一个人想着一个人 提交于 2019-12-12 18:27:27

问题


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?


回答1:


You can use

(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

if you're counting characters, you can also just do

(start_date1..end_date1).to_set & start_date2..end_date2

but I think the original version is clearer.



来源:https://stackoverflow.com/questions/2917538/how-do-i-find-common-dates-in-two-ranges

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!