Tim Hibbard

CEO for EnGraph software
posts - 629 , comments - 1678 , trackbacks - 459

My Links

News



Add to Google

Twitter












Tag Cloud

Article Categories

Archives

Post Categories

Image Galleries

EnGraph Blogs

Links

Other

Roll

C# code to get generic list of dates between starting and ending date

This code snippet will return a generic list of DateTime containing the dates between a starting date and ending date:

private List<DateTime> GetDateRange(DateTime StartingDate, DateTime EndingDate) { if (StartingDate > EndingDate) { return null; } List<DateTime> rv = new List<DateTime>(); DateTime tmpDate = StartingDate; do { rv.Add(tmpDate); tmpDate = tmpDate.AddDays(1); } while (tmpDate <= EndingDate); return rv; }

To view this code in action, copy and paste the following code into SnippetCompiler:

DateTime StartingDate = DateTime.Parse("02/25/2007"); DateTime EndingDate = DateTime.Parse("03/06/2007"); foreach (DateTime date in GetDateRange(StartingDate,EndingDate)) { WL(date.ToShortDateString()); }

And it will return:
2/25/2007
2/26/2007
2/27/2007
2/28/2007
3/1/2007
3/2/2007
3/3/2007
3/4/2007
3/5/2007
3/6/2007

Technorati tags: , , ,

Print | posted on Thursday, March 1, 2007 12:54 PM | Filed Under [ .NET ]

Feedback

Gravatar

# re: C# code to get generic list of dates between starting and ending date

Times like this are why the GOod Lord gave us C# iterators:

IEnumerable<DateTime> GetDateRange(DateTime StartingDate, DateTime EndingDate)
{
while (StartingDate <= EndingDate)
{
yield return StartingDate;
StartingDate = StartingDate.AddDays(1);
}
}
3/2/2007 11:22 AM | James Curran
Gravatar

# re: C# code to get generic list of dates between starting and ending date

vsxv very badaa
11/3/2009 2:41 AM | balu
Gravatar

# re: C# code to get generic list of dates between starting and ending date

vsxv very bad code
11/3/2009 2:41 AM | balu
Gravatar

# re: C# code to get generic list of dates between starting and ending date

Very Helpful, but what does it means by-

WL(date.ToShortDateString());

I mean what is "WL"?
2/27/2011 4:07 AM | Akter
Gravatar

# re: C# code to get generic list of dates between starting and ending date

Oh...Thank God....Thank u very much...Very helpful..
4/18/2011 5:05 AM | Ajith Puthur
Gravatar

# re: C# code to get generic list of dates between starting and ending date

it really good, saved my time..
7/19/2011 10:38 AM | Kavita
Gravatar

# re: C# code to get generic list of dates between starting and ending date

Thanks dude, you saved my morning!
12/1/2011 5:01 AM | Nicolas
Gravatar

# re: C# code to get generic list of dates between starting and ending date

Very helpful code. Thanks.
2/16/2012 11:49 AM | freestyler
Gravatar

# re: C# code to get generic list of dates between starting and ending date

pls send me code how to compare product manufactring date to expiry date,,,
so how to display expiry date and pop up message show to expiry date
2/25/2012 1:29 AM | DEEPAK
Gravatar

# re: C# code to get generic list of dates between starting and ending date

thanks so much
7/28/2012 12:53 PM | zamary shahab
Gravatar

# re: C# code to get generic list of dates between starting and ending date

Awesome great :)
2/7/2013 1:15 AM | Rashid Imran Bilgrami
Gravatar

# re: C# code to get generic list of dates between starting and ending date

I'm looking this solution for my desktop software. Appreciate to get this solving.
5/12/2013 3:40 AM | Nashir
Gravatar

# re: C# code to get generic list of dates between starting and ending date

WL means Console.WriteLine
4/23/2014 3:47 AM | majid
Gravatar

# re: C# code to get generic list of dates between starting and ending date

nice and simple
4/28/2014 3:57 AM | majid
Gravatar

# re: C# code to get generic list of dates between starting and ending date

DateTime StartingDate = DateTime.Parse("02/25/2007");
DateTime EndingDate = DateTime.Parse("03/06/2007");
foreach (DateTime date in GetDateRange(StartingDate, EndingDate))
{
Console.Write(date.ToShortDateString());
DropDownList1.Items.Add(date.ToShortDateString());
}
5/19/2014 6:51 AM | Hossein
Gravatar

# re: C# code to get generic list of dates between starting and ending date

helpful code, thanks a lot
8/9/2014 3:55 AM | anhvh
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 
 

Powered by: