954,178 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to calculate difference between two dates and ignoring weekend days

Hi,

I am new to c#. Now i m currently working in a leave system project, in this i have two popup calendar to select the leave-start date and leave-end date for employee. If employee select more than 5 days, weekend dates should not be calculate as a leave credit. I m using the following code to find the difference.

DateTime dt = Convert.ToDateTime(txtfrmdate.SelectedDate.ToShortDateString());
        DateTime dt1 = Convert.ToDateTime(txtenddate.SelectedDate.ToShortDateString());
        TimeSpan diff = dt1.Subtract(dt);
        int days = diff.Days + 1;


In this if we select dates 11/26/2009 to 12/2/2009, result will be 7. here the weekend dates also calculated. But i want to ignore the weekend days.

Please can any one help me how to do this.

praveenfds
Newbie Poster
5 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Try this.

DateTime startDate = new DateTime( 2004 , 3 , 18 ) ;
                  DateTime endDate   = new DateTime( 2004 , 3 , 22 ) ;

                  int countDays = 0;

                  DateTime dateIterator = startDate ;

                  while( dateIterator < endDate.AddDays(1) )
                  {
                        if( dateIterator.DayOfWeek != DayOfWeek.Saturday && dateIterator.DayOfWeek != DayOfWeek.Sunday )
                              countDays ++;

                        dateIterator = dateIterator.AddDays(1);
                  }

                  Console.Out.WriteLine( "Number of working days between {0} and {1}\nis {2} days" , startDate , endDate , countDays );
                  Console.Out.WriteLine( "Press Return To Finish" );
                  Console.In.Read();


Here change the start date and end dates accordingly.

Ramesh S
Posting Pro
580 posts since Jun 2009
Reputation Points: 165
Solved Threads: 113
 
private int DaysIgnoreWeekends(DateTime dtst, DateTime dtend)
        { 
            TimeSpan days = dtend.Subtract(dtst);
            int count = 0;
            for (int a = 0; a < days.Days + 1; a++)
            {
                if (dtst.DayOfWeek != DayOfWeek.Saturday && dtst.DayOfWeek != DayOfWeek.Sunday)
                {
                    count++;
                }
                dtst = dtst.AddDays(1.0);
            }
            return count;
        }


this is a little function

jbisono
Posting Pro in Training
442 posts since May 2009
Reputation Points: 71
Solved Threads: 59
 

oops sorry about that i didn't notice that mr. ramesh post a really nice solution. anyway i think you have the point.

jbisono
Posting Pro in Training
442 posts since May 2009
Reputation Points: 71
Solved Threads: 59
 
private int DaysIgnoreWeekends(DateTime dtst, DateTime dtend)
        { 
            TimeSpan days = dtend.Subtract(dtst);
            int count = 0;
            for (int a = 0; a < days.Days + 1; a++)
            {
                if (dtst.DayOfWeek != DayOfWeek.Saturday && dtst.DayOfWeek != DayOfWeek.Sunday)
                {
                    count++;
                }
                dtst = dtst.AddDays(1.0);
            }
            return count;
        }

this is a little function


Hi,
Thank you very much, this code works properly

praveenfds
Newbie Poster
5 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You