使用日期时间范围填充列表的简单方法 [英] Simple way to populate a List with a range of Datetime

查看:35
本文介绍了使用日期时间范围填充列表的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种解决此问题的简单方法.

I trying to find a simple way to solve this.

我有一个初始日期和一个最终日期.

I have a Initial Date, and a Final Date.

我想用给定时间段中的每个日期生成一个 List< Datetime> .

And I want to generate a List<Datetime> with each of the dates in a given period.

示例:初始日期为"2013/12/01",最终日期为"2013/12/05".

Example : Initial Date is "2013/12/01" and Final Date is "2013/12/05".

我想自动填充一个列表

"2013/12/01"
"2013/12/02"
"2013/12/03"
"2013/12/04"
"2013/12/05"


你会建议我什么?


What would you suggest me?

谢谢

推荐答案

var startDate = new DateTime(2013, 12, 1);
var endDate = new DateTime(2013, 12, 5);

var dates = Enumerable.Range(0, (int)(endDate - startDate).TotalDays + 1)
                      .Select(x => startDate.AddDays(x))
                      .ToList();

这篇关于使用日期时间范围填充列表的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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