Click here to Skip to main content
13,091,134 members (92,435 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


22 bookmarked
Posted 8 Nov 2011

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

, 25 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Find the count of a weekday between two dates without iterating/looping

When you want number of Thursdays or any other week day between two dates, there is no direct function in .NET. To fill the gap, I have written the following function that gives the count of a weekday between two given dates. This function is constructed without using iteration/loop statements.

public static int  findWeekCount(DateTime startDate, DateTime toDate, DayOfWeek Week)
    int _daysBetweenDates = (toDate - startDate).Days;  // days between start date and end date
    int _adjustingDays = (7 + (int)Week - (int)startDate.DayOfWeek) % 7; // days between week of start date and searching week
    int _oddDays = _daysBetweenDates % 7; // remainder of total days after dividing 7
    int _completeWeekTurns = (_daysBetweenDates) / 7;
    int _addTurns = _oddDays >= _adjustingDays  ? 1 : 0;
    return _completeWeekTurns + _addTurns;


DateTime sDate = new DateTime(2011, 09, 1);
DateTime eDate = new DateTime(2011, 11, 30);
DayOfWeek fWeek = DayOfWeek.Monday;
int totCount = findWeekCount(sDate, eDate, fWeek);
MessageBox.Show(totCount.ToString()); //Result : 13 (Mondays)

Hope this is useful. Any alternative is highly appreciated.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ravi LVS
India India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionHow get the list of sundays between 2 specified dates in Pin
sushant.khot3126-Aug-12 11:35
membersushant.khot3126-Aug-12 11:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170813.1 | Last Updated 25 Dec 2011
Article Copyright 2011 by Ravi LVS
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid