## 6 replies

Last post Oct 30, 2013 12:05 AM by Prajwal14

• Prajwal14

Member

5 Points

15 Posts

### Extracting No Of Weekdays(e.g. Monday) between two dates

Hi,

I want to get no of mondays or tuesdays between two dates. I have developed a function for the same as below -

TimeSpan ts = end - start;         // End is End date and Start is Start date

int count = (int)Math.Floor(((ts.TotalDays)) / 7);

int remainder = (int)(ts.TotalDays % 7);

int sinceLastDay = (int)(end.DayOfWeek - day);

if (sinceLastDay < 0) sinceLastDay += 7;

if (remainder >= sinceLastDay) count++;

But the above function returns no.of mondays n all without considering Start date and End date. I want Start and End date to be alsoget considered and give the exact output. Can anyone suggest anythig?

• smirnov

All-Star

35159 Points

9075 Posts

### Re: Extracting No Of Weekdays(e.g. Monday) between two dates

The above code works correct

DayOfWeek day = DayOfWeek.Monday;
DateTime start = new DateTime(2013, 9, 30);
DateTime end = new DateTime(2013, 10, 29);

TimeSpan ts = end - start; // Total duration
int count = (int)Math.Floor(ts.TotalDays / 7); // Number of whole weeks
int remainder = (int)(ts.TotalDays % 7); // Number of remaining days
int sinceLastDay = (int)(end.DayOfWeek - day); // Number of days since last [day]
if (sinceLastDay < 0) sinceLastDay += 7; // Adjust for negative days since last [day]

// If the days in excess of an even week are greater than or equal to the number days since the last [day], then count this one, too.
if (remainder >= sinceLastDay) count++;

Console.WriteLine(count);

----

output is 5 which is right.

• Prajwal14

Member

5 Points

15 Posts

### Re: Extracting No Of Weekdays(e.g. Monday) between two dates

Yes the code is correct for the days between Start Date and End Date, but it does not consider days of Start Date and End Date.. Suppose there is Monday on start date and Friday on End date, the above code does not count these 2 Monday and Friday into the count.. IF the no of mondays between start date and end date is 5 then it should also consider the Monday of Start Date and give the result as 6.

• smirnov

All-Star

35159 Points

9075 Posts

### Re: Extracting No Of Weekdays(e.g. Monday) between two dates

I think there is some misunderstanding of what the code does.

It counts only one specific day of week. Not two.

#### Prajwal14

Suppose there is Monday on start date and Friday on End date, the above code does not count these 2 Monday and Friday into the count..

If it counts Mondays, then it will not count Fridays and vice versa.

#### Prajwal14

IF the no of mondays between start date and end date is 5 then it should also consider the Monday of Start Date and give the result as 6.

As you can see from my example (above) there are 2 dates, 9/30 and 10/29. There are 5 Mondays between both dates, not 6. 9/30 is Monday and it is included in that count of 5.

• Rion William...

All-Star

113020 Points

18361 Posts

MVP

### Re: Extracting No Of Weekdays(e.g. Monday) between two dates

Oct 29, 2013 08:32 AM|Rion Williams|LINK

Have you considered iterating through your Dates and simply checking if the Date is a Monday or Tuesday and incrementing accordingly?

```//Example Dates
DateTime end = DateTime.Now;

//Mondays and Tuesdays counter
int mondaysAndTuesdays = 0;

//Temp Date (don't count the first day)

while (dt < end)
{
//Increment if it is Monday or Tuesday
if (dt.DayOfWeek == DayOfWeek.Monday || dt.DayOfWeek == DayOfWeek.Tuesday)
{
mondaysAndTuesdays++;
}
}

//Access your total number of Mondays and Tuesdays here```

• Paul Linton

Star

9555 Points

2784 Posts

### Re: Extracting No Of Weekdays(e.g. Monday) between two dates

Oct 29, 2013 10:51 PM|Paul Linton|LINK

It is not clear, to me at least, what you are trying to count.  Can you give some examples and what answer you are expecting?  Even the title of the post is ambiguous to me.  Do you want to count all weekdays or just Mondays?

Then in the OP you want to get 'no of mondays or tuesdays'.  Does this mean for October 22 to October 29 the answer should be

1 - the number of Mondays

2 - the number of Tuesdays

3 - the number of Mondays or Tuesdays

6 - the number of Weekdays

It is not clear which would be the correct answer.  Or is it that sometimes you want one of these answers and sometimes another one, depending on another parameter?

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
• Prajwal14

Member

5 Points

15 Posts