views:

260

answers:

3

Hi Everyone,

I am hoping that this is possible and easy! As the title says, I want to specify two dates and get a data set of dates by specifying the start and end date, then be able to bind that to a DropDownList control on my page..

If it isn't possible (which is a damn shame) does anyone have recommendations or best practices for doing this, as that sort of functionality appears on lots of sites.

Thanks in advance for any help! Pete

A: 

Apologies! I have since discovered that I can use DateTime span.. thanks go to this article on asp.net forums:

http://forums.asp.net/p/944596/1137162.aspx#1137162

peteski22
+2  A: 

Try this:

        DateTime start = new DateTime(2009, 1, 1);
        DateTime end = new DateTime(2009, 3, 1);
        var dates = Enumerable.Range(0, (end - start).Days).Select(x => start.AddDays(x));
Jakob Christensen
+2  A: 

I would think a simple way of doing this is to use a loop to iterate through the date range and add each date to the DropDownList. Something like:

while(startDate.Date <= endDate.Date)
{
    myDropDownList.Items.Add(new ListItem(startDate.ToShortDateString()));
    startDate = startDate.AddDays(1);
}
Andy Rose
Yeah, thats pretty succinct! Nice one.
peteski22