How to Bind a Date Range in a Dropdownlist Excluding Sunday

In this Blog I'm going tell you How to bind a date range in a Dropdownlist excluding Sunday at both starting date and end date.
Suppose I have to bind 4 to 7 days starting from today's date.  
 
Like today's date is  20th May 2015, then the displayed date will be 25th to 28th May 2015.  Here I'm excluding today's date. stimeperiod = 3; etimeperiod = 7;
 
 So in code behind
  1. int stimeperiod = 3;       
  2.  int etimeperiod = 7;      
  3.       
  4. //For Starting Date      
  5.             DateTime TODAYDATE = DateTime.Now;      
  6.       
  7.                 
  8.             DateTime StartDate = DateTime.Now; ;      
  9.             int DayInterval = 1;      
  10.             int count = 0;      
  11.             while (TODAYDATE.AddDays(DayInterval) <= DateTime.Now.AddDays(stimeperiod))      
  12.             {      
  13.                 if (TODAYDATE.AddDays(DayInterval).DayOfWeek != DayOfWeek.Sunday)      
  14.                 {      
  15.                     StartDate = TODAYDATE.AddDays(DayInterval);      
  16.                     TODAYDATE = TODAYDATE.AddDays(DayInterval);      
  17.                 }      
  18.                 else      
  19.                 {      
  20.                     TODAYDATE = TODAYDATE.AddDays(DayInterval);      
  21.                     stimeperiod = stimeperiod + 1;      
  22.                     count++;      
  23.                 }      
  24.             }      
  25.       
  26.       
  27.             //For Ending Date      
  28.             DateTime EndDate = DateTime.Now.AddDays(etimeperiod);      
  29.             if (count > 0) { EndDate = EndDate.AddDays(1); }      
  30.       
  31.             List<DateTime> datelist = new List<DateTime>();      
  32.             while (StartDate.AddDays(DayInterval) <= EndDate)      
  33.             {      
  34.                 if (StartDate.AddDays(DayInterval).DayOfWeek != DayOfWeek.Sunday)      
  35.                 {      
  36.                     StartDate = StartDate.AddDays(DayInterval);      
  37.                     datelist.Add(StartDate);      
  38.       
  39.                 }      
  40.                 else      
  41.                 {      
  42.                     EndDate = EndDate.AddDays(1);      
  43.                     StartDate = StartDate.AddDays(DayInterval);      
  44.                 }      
  45.             }      
  46.                   
  47.             DDL_Date.DataSource = datelist;      
  48.             DDL_Date.DataBind();      
  49.             DDL_Date.Items.Insert(0, new ListItem("--Select Date--""0"));   
And in aspx page
  1. <asp:DropDownList runat="server" ID="DDL_Date" DataTextFormatString="{0:D}"  />   
 Output :-


Hope you liked it... :)
 
All Comments are welcomed.. 
Ebook Download
View all
Learn
View all