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. //For Starting Date
  4. DateTime TODAYDATE = DateTime.Now;
  5. DateTime StartDate = DateTime.Now; ;
  6. int DayInterval = 1;
  7. int count = 0;
  8. while (TODAYDATE.AddDays(DayInterval) <= DateTime.Now.AddDays(stimeperiod))
  9. {
  10. if (TODAYDATE.AddDays(DayInterval).DayOfWeek != DayOfWeek.Sunday)
  11. {
  12. StartDate = TODAYDATE.AddDays(DayInterval);
  13. TODAYDATE = TODAYDATE.AddDays(DayInterval);
  14. }
  15. else
  16. {
  17. TODAYDATE = TODAYDATE.AddDays(DayInterval);
  18. stimeperiod = stimeperiod + 1;
  19. count++;
  20. }
  21. }
  22. //For Ending Date
  23. DateTime EndDate = DateTime.Now.AddDays(etimeperiod);
  24. if (count > 0) { EndDate = EndDate.AddDays(1); }
  25. List<DateTime> datelist = new List<DateTime>();
  26. while (StartDate.AddDays(DayInterval) <= EndDate)
  27. {
  28. if (StartDate.AddDays(DayInterval).DayOfWeek != DayOfWeek.Sunday)
  29. {
  30. StartDate = StartDate.AddDays(DayInterval);
  31. datelist.Add(StartDate);
  32. }
  33. else
  34. {
  35. EndDate = EndDate.AddDays(1);
  36. StartDate = StartDate.AddDays(DayInterval);
  37. }
  38. }
  39. DDL_Date.DataSource = datelist;
  40. DDL_Date.DataBind();
  41. 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..