Wednesday, November 10, 2010

Select Date with leaving Weekend in Asp.Net

Hey All,
     For Past week I m worrying abt rendering the day counts in ASP.Net jst now found the solution .
For Example I vl let yo knw the scenario if I want to prepare leave form hence I got to design a calender with start date and end date to it what I m going to do just give yo the piece of code where weekend will be neg lated in the coding so when ever yo come across this scenario yo vl thank to me ...:-)


public void checkmonth()
    {
        TimeSpan timespan = Convert.ToDateTime(cal2).Subtract(Convert.ToDateTime(cal1));
        DateTime date1 = Convert.ToDateTime(cal1);
        DateTime date2 = Convert.ToDateTime(cal2);
        //NoOfDays.Text = Convert.ToString(timespan.Days + 1);
        long        wholeWeeks = ((long)Math.Round(Math.Floor(timespan.TotalDays))) / 7;
   DateTime    dateCount  = date1.AddDays(wholeWeeks * 7);
   int         endDays    = 0;


        
   while (dateCount.Date <= date2.Date)
   {
      switch (dateCount.DayOfWeek)
      {
         case DayOfWeek.Saturday:
         case DayOfWeek.Sunday:
            break;
         default:
            endDays++;
            break;
         }
         dateCount = dateCount.AddDays(1);
   }
   NoOfDays.Text=Convert.ToString(wholeWeeks * 5 + endDays);

            }

Happy Coding

No comments: