Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate no of weekdays in date range

Status
Not open for further replies.

James1981

Programmer
Nov 2, 2002
76
0
0
US
Hi,

Is there an easy way to calculate the no of weekdays (not weekends) in a specified date range?

Cheers
James
 
Hi,

Following does the job - maybe not so elegant - but it works!

Code:
   DateTime start = new DateTime(<start date>); //see DateTime constructor for various formats of <start date>
   DateTime end = new DateTime(<end date>);
   int count = 0;
   for (DateTime dt=start; dt<end; dt=dt.AddDays(1))
   {
      if (dt.DayOfWeek!=DayOfWeek.Saturday && dt.DayOfWeek!=DayOfWeek.Sunday)
         count++;  
   }
   //count is now num of weekdays in period

Cheers,

Gary
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top