How can I calculate the Number of Weekends between two dates

```I need to be able to figure out the number of Saturdays and the number of Sundays between two dates.  I know that I can do this by looping through the series of dates between the start date and end date.  Years ago I saw a piece of code that an ex-co worker wrote that accomplished this task using a formula.  The formula was extemely effecient.  Unfortunately I no longer have access to this code.  Does anyone have an algorithm that will perform this calculation that I can make use of?
Thanks```
 0
mikener
8/4/2007 3:55:24 AM
📁 asp.net.getting-started
📃 91979 articles.
⭐ 3 followers.

💬 14 Replies
👁️‍🗨️ 543 Views

```I don't have a formula for the calculation, but couldn't you just determine which day is the start date (from a datetime, that is not hard at all) and calculate how many days there are from the start date to the end date (should also be an easy calculation to make). Since you know that there is one saturday and one sunday each seven days, you could calculate how many full weeks there are between the two dates. Then you could either use the rest from the calculation to see if there is a saturday and/or a sunday from the startdate, by adding only the rest, or you could use the end dates day of week to determine this.
Good luck!Dont forget to click "Mark as Answer" on the post that helped you.This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped. ```
 0
robert
8/4/2007 9:17:43 AM
` Try a function along the lines of   private int CalculateWeekends(DateTime DateTime1, DateTime DateTime2)  {    int iReturn = 0;    TimeSpan xTimeSpan;    if (DateTime2 > DateTime1)      xTimeSpan = DateTime2.Subtract(DateTime1);    else      xTimeSpan = DateTime1.Subtract(DateTime2);    int iDays = 5 + System.Convert.ToInt32(xTimeSpan.TotalDays);    iReturn = (iDays / 7);    return iReturn;  } Don't forget to click "Mark as Answer" on the post that helped you.This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped. `
 0
TATWORTH
8/4/2007 9:45:07 AM
`Is the issue resolved? If so, please mark it as such, so that everyone knows not to read this thread as other than reference, and so that those with a similar problem knows they can use the thread as a reference. If the issue is not resolved, please keep up the communication and I'm sure you'll be helped in solving the issue.Dont forget to click "Mark as Answer" on the post that helped you.This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped. `
 0
robert
8/6/2007 6:32:14 AM
```Sorry for the delay. I was out of the office. The formula appears to be close. I am just testing it now.
Once I get the correct answer I will post it.
```
 0
mikener
8/6/2007 11:54:41 AM
```This method is close.  I haven't worked out why it does not work as of yet.
Using Start Date of 8/1/2007 and an End Date of 8/9/2007 this method correctly determines that there is one weekend between the two dates.
Using Start Date of 8/1/2007 and an End Date of 8/10/2007 this method incorrectly determines that there are two weekends between the two dates.
Using Start Date of 7/31/2007 and an End Date of 8/09/2007 this method incorrectly determines that there are two weekends between the two dates.
Any help would be much appreciated.
Thanks```
 0
mikener
8/6/2007 2:21:17 PM
```It might not be the best solution considering performance, nor considering beauty of code, but I think it works (observe that the method includes the start day and excludes the end day):         private const int MONDAY = 1;
private const int TUESDAY = 2;
private const int WEDNESDAY = 3;
private const int THURSDAY = 4;
private const int FRIDAY = 5;
private const int SATURDAY = 6;
private const int SUNDAY = 7;

/// <summary>
/// Observe that the method calculates by including the start day, but excludes the end day
/// </summary>
/// <param name="startDate"></param>
/// <param name="endDate"></param>
/// <returns></returns>
private void GetWeekendDaysBetween(DateTime startDate, DateTime endDate,out int saturdays,out int sundays)
{
saturdays = -1;
sundays = -1;
if (endDate < startDate)
return;
TimeSpan timeBetween = endDate.Subtract(startDate);
int weekendsBetween = timeBetween.Days / 7;
sundays = weekendsBetween;
saturdays = weekendsBetween;
int startDay = GetDayOfWeekNumber(startDate.DayOfWeek);
int endDay = GetDayOfWeekNumber(endDate.DayOfWeek);
if (startDay > endDay)
{
sundays++;
saturdays += (startDay < SUNDAY) ? 1 : 0;
}
else if (startDay < endDay)
{
//We don't have to care about sundays here, since we are excluding the last day
//There will only be another saturday, if the end day is a sunday
saturdays += (endDay == SUNDAY) ? 1 : 0;
}
}

/// <summary>
/// Since I don't think it's a good idea to rely on the face that the enums have specific values, I wrote this method
/// If you are satisfied with using the integer value of the enums, just remember that Sundays will then have the value 0
/// </summary>
/// <param name="day"></param>
/// <returns></returns>
private int GetDayOfWeekNumber(DayOfWeek day)
{
switch (day)
{
case DayOfWeek.Monday:
return MONDAY;
case DayOfWeek.Tuesday:
return TUESDAY;
case DayOfWeek.Wednesday:
return WEDNESDAY;
case DayOfWeek.Thursday:
return THURSDAY;
case DayOfWeek.Friday:
return FRIDAY;
case DayOfWeek.Saturday:
return SATURDAY;
case DayOfWeek.Sunday:
return SUNDAY;
default:
throw new ArgumentException("Invalid day!");
}
}
Dont forget to click "Mark as Answer" on the post that helped you.This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped. ```
 0
robert
8/6/2007 4:25:53 PM
```I will try to incorporate this into my code as soon as I get a chance.  There is one change that I noticed right off that while no big deal may or may note help you out.  The method GetDayOfWeekNumber does not need the switch statement. The method needs only one line:
return (int) day + 1;
This is not much of a help, but I thought that I should mention it.
Thanks again. As I said, I will test and mark this as answered as soon as I get chance (most likely tomorrow).```
 0
mikener
8/6/2007 7:22:27 PM
`For two reasons, that wouldn't do good. First and foremost, sunday is 0 and monday is 1, and the rest of the code builds on the fact that sunday has the highest, not the lowest, number. The other reason is that I did not want to rely on the enum integers, as I wrote as comments in the text.Dont forget to click "Mark as Answer" on the post that helped you.This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped. `
 0
robert
8/6/2007 7:53:33 PM
```Sorry for the mix up. I skimmed the code and not the comments.  I was in a hurry to give you some feedback before I had a chance to test the code.  My mistake. I tested the code. It works.
Thanks
```
 0
mikener
8/7/2007 11:54:07 AM
`It's okay. I actually like getting comments and feedback, so that I can improve. I am currently trying to work a lot on my grip on the english language so that I can use it better to be direct and consise without being rude or harsh. I apologize if my above post was considered as any of the latter.Dont forget to click "Mark as Answer" on the post that helped you.This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped. `
 0
robert
8/7/2007 7:49:31 PM
```No problem. I really do appreciate the help.
Thanks again.```
 0
mikener
8/8/2007 12:11:50 AM
`The above code does not work well in all the situations. For example try the date range "2008, 1, 3" to  "2008, 1, 6".  This gives the answer 1(sum of saturdays and sundays) . But there are actually two weekend days in between. Fed up with coding? Just relax `
 0
robint84
10/14/2008 5:05:09 AM
`Actually, the answer 1 is entirely correct. January 6th 2008 is a sunday. If you read the post which contains the code you'll see "(observe that the method includes the start day and excludes the end day)". Thus, it counts thursday, friday and saturday which means that, in the case of january 3rd to january 6th 2008, the sum of all saturdays and sundays is 1. Dont forget to click "Mark as Answer" on the post that helped you.This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped. `
 0
robert
10/23/2008 11:24:24 AM
`I am really sorry. I didn't notice that. It works fine :)Fed up with coding? Just relax `
 0
robint84
10/23/2008 4:57:05 PM