如何从开始日期和结束日期查找日期列表 [英] How to find a list of Dates from a Start-Date and End-Date

查看:112
本文介绍了如何从开始日期和结束日期查找日期列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

查找指定范围内的缺失日期

创建两个日期之间的所有日期的数组或列表

从数据类型为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆