13,893,974 members
Alternative Tip/Trick
alternative version

#### Stats

5.4K views
Posted 11 Nov 2011
Licenced CPOL

# Find the count of a weekday between two dates without iterating/looping

, 11 Nov 2011
Is it easier to start from the next targeted day? Subtract that date from the end date. If the result is negative, return 0 otherwise return the quotient of the difference in days between the two dates divided by 7 plus 1.public static int findWeekCount(DateTime startDate, DateTime toDate,...

Is it easier to start from the next targeted day? Subtract that date from the end date. If the result is negative, return 0 otherwise return the quotient of the difference in days between the two dates divided by 7 plus 1.

```public static int findWeekCount(DateTime startDate, DateTime toDate, DayOfWeek dayOfWeek)
{
DateTime firstTargetDate = startDate+ TimeSpan.FromDays(7 + (int)dayOfWeek - ((int)startDate.DayOfWeek) % 7);
return (toDate - firstTargetDate).Days < 0 ? 0 : (((toDate - firstTargetDate).Days) / 7) + 1 ;
}```

## Share

 Student Wales
No Biography provided

## You may also be interested in...

 Pro

 First Prev Next
 Mathematically your suggestion and mine are same and one. O... Ravi LVS10-Nov-11 15:39 Ravi LVS 10-Nov-11 15:39
 Last Visit: 18-Mar-19 7:58     Last Update: 18-Mar-19 7:58 Refresh 1