Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to populate a List with a range of Datetime

Tags:

c#

.net

datetime

I trying to find a simple way to solve this.

I have a Initial Date, and a Final Date.

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


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

And I want to automatically populate a list with

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

What would you suggest me?

Thanks

like image 505
Guilherme de Jesus Santos Avatar asked Dec 04 '13 18:12

Guilherme de Jesus Santos


People also ask

How do you make a datetime range in Python?

Using Pandas to Create a List of Range of Dates in Python. We will use the date_range() function of pandas in which we will pass the start date and the number of days after that (known as periods). Here we have also used the datetime library to format the date so that we can output the date in this format DD-MM-YY .


2 Answers

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();
like image 58
MarcinJuraszek Avatar answered Sep 23 '22 11:09

MarcinJuraszek


for(var day = from.Date; day.Date <= end.Date; day = day.AddDays(1))
{
     list.Add(day);
}
like image 24
Andrei V Avatar answered Sep 22 '22 11:09

Andrei V