找到一个给定的范围缺少的日期 [英] Find missing dates for a given range
本文介绍了找到一个给定的范围缺少的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图寻找失踪的两个日期时间变量之间的日期创建DateTime对象的集合。
I'm trying to find missing dates between two DateTime variables for a collection of DateTimes.
例如。
Collection
2010-01-01
2010-01-02
2010-01-03
2010-01-05
DateRange
2010-01-01 -> 2010-01-06
会给我一个列表< DateTime的>
的
2010-01-04
2010-01-06
我能想到的几个是实现这一点,但什么都不干净和简单
I can think of a few was of implementing this but nothing clean and simple
任何想法
推荐答案
我能想到很多执行本办法的,例如:
I can think of a lot of ways of implementing this, e.g.:
DateTime[] col = { new DateTime(2010, 1, 1),
new DateTime(2010, 1, 2),
new DateTime(2010, 1, 3),
new DateTime(2010, 1, 5)};
var start = new DateTime(2010, 1, 1);
var end = new DateTime(2010, 1, 6);
var range = Enumerable.Range(0, (int)(end - start).TotalDays + 1)
.Select(i => start.AddDays(i));
var missing = range.Except(col);
和你可以把范围,塞给她一个扩展法
And you could put the range-stuff into an Extension-Method
public static class extensions
{
public static IEnumerable<DateTime> Range(this DateTime startDate, DateTime endDate)
{
return Enumerable.Range(0, (int)(endDate - startDate).TotalDays + 1)
.Select(i => startDate.AddDays(i));
}
}
那么这将是简单
Then it would be simply
DateTime[] col = { new DateTime(2010, 1, 1),
new DateTime(2010, 1, 2),
new DateTime(2010, 1, 3),
new DateTime(2010, 1, 5)};
var start = new DateTime(2010, 1, 1);
var end = new DateTime(2010, 1, 6);
var missing = start.Range(end).Except(col);
但也许这并不是一个高性能的解决方案: - )
But maybe this is not a high-performance-solution :-)
这篇关于找到一个给定的范围缺少的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文