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?

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++;

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.

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.

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

//Example Dates
DateTime start = DateTime.Now.AddDays(-30);
DateTime end = DateTime.Now;
//Mondays and Tuesdays counter
int mondaysAndTuesdays = 0;
//Temp Date (don't count the first day)
DateTime dt = start.AddDays(1);
//Iterate through your dates
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

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?

Yes, i was bit unclear as i have 7 checkboxes for the 7 days, user can select as many as can.. and the problem is solved, the code is correct.
@All - the scenario is - suppose it is Monday on Start date and user wants to count no. of mondays between Start Date and End Date then it was NOT Considering the Monday OF START DATE.. This was my problem and it is solved now as the code
is correct.. Thank you all

Member

5 Points

15 Posts

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

Oct 29, 2013 01:22 AM|Prajwal14|LINK

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?

All-Star

35159 Points

9075 Posts

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

Oct 29, 2013 04:07 AM|smirnov|LINK

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.

Member

5 Points

15 Posts

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

Oct 29, 2013 08:06 AM|Prajwal14|LINK

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.

All-Star

35159 Points

9075 Posts

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

Oct 29, 2013 08:26 AM|smirnov|LINK

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

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

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

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.

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?

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?

freepdf.Member

5 Points

15 Posts

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

Oct 30, 2013 12:05 AM|Prajwal14|LINK

Yes, i was bit unclear as i have 7 checkboxes for the 7 days, user can select as many as can.. and the problem is solved, the code is correct.

@All- the scenario is - suppose it is Monday on Start date and user wants to count no. of mondays between Start Date and End Date then it was NOT Considering the Monday OF START DATE.. This was my problem and it is solved now as the code is correct.. Thank you all