Geeks With Blogs

News



Add to Google

Tim Hibbard CEO for EnGraph software

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: , , ,
Posted on Thursday, March 1, 2007 12:54 PM | Back to top


Comments on this post: C# code to get generic list of dates between starting and ending date

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
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);
}
}
Left by James Curran on Mar 02, 2007 11:22 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
vsxv very badaa
Left by balu on Nov 03, 2009 2:41 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
vsxv very bad code
Left by balu on Nov 03, 2009 2:41 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
Very Helpful, but what does it means by-

WL(date.ToShortDateString());

I mean what is "WL"?
Left by Akter on Feb 27, 2011 4:07 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
Oh...Thank God....Thank u very much...Very helpful..
Left by Ajith Puthur on Apr 18, 2011 5:05 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
it really good, saved my time..
Left by Kavita on Jul 19, 2011 10:38 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
Thanks dude, you saved my morning!
Left by Nicolas on Dec 01, 2011 5:01 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
Very helpful code. Thanks.
Left by freestyler on Feb 16, 2012 11:49 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
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
Left by DEEPAK on Feb 25, 2012 1:29 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
thanks so much
Left by zamary shahab on Jul 28, 2012 12:53 PM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
Awesome great :)
Left by Rashid Imran Bilgrami on Feb 07, 2013 1:15 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
I'm looking this solution for my desktop software. Appreciate to get this solving.
Left by Nashir on May 12, 2013 3:40 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
WL means Console.WriteLine
Left by majid on Apr 23, 2014 3:47 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
nice and simple
Left by majid on Apr 28, 2014 3:57 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
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());
}
Left by Hossein on May 19, 2014 6:51 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
helpful code, thanks a lot
Left by anhvh on Aug 09, 2014 3:55 AM

# re: C# code to get generic list of dates between starting and ending date
Requesting Gravatar...
thanks a lot. Its helpful code
Left by MAruthi on Nov 18, 2014 10:59 PM

Your comment:
 (will show your gravatar)


Copyright © Tim Hibbard | Powered by: GeeksWithBlogs.net | Join free