如何从开始日期和结束日期查找日期列表 [英] How to find a list of Dates from a Start-Date and End-Date
本文介绍了如何从开始日期和结束日期查找日期列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从数据类型为 DateTime
的两个日期开始,表示开始日期
和结束日期
。
From two dates of data type DateTime
representing a date of start
and a date of end
.
我需要返回一个包含DateTime格式的所有日期的数组或List
I need return an array or List which contain all the dates in DateTime format.
出于性能原因考虑使用更快的代码。有人可以用示例源代码向我指出正确的方向吗?
Considering the faster code for performance reason. Could anyone please point me out in the right direction with sample source code?
示例
输入
startDate = 25/01/2013
endDate = 31/01/2013
结果
25/01/2013
26/01/2013
27/01/2013
28/01/2013
29/01/2013
30/01/2013
31/01/2013
推荐答案
您可以使用 Enumerable.Range
:
var startDate = new DateTime(2013, 1, 25);
var endDate = new DateTime(2013, 1, 31);
int days = (endDate - startDate).Days + 1; // incl. endDate
List<DateTime> range = Enumerable.Range(0, days)
.Select(i => startDate.AddDays(i))
.ToList();
这篇关于如何从开始日期和结束日期查找日期列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文