Click here to Skip to main content
12,349,363 members (33,235 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

3.7K views
Posted

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

, 11 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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 ;
}

License

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

Share

About the Author

George Swan
Student
Wales Wales
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMathematically your suggestion and mine are same and one. O... Pin
Ravi LVS10-Nov-11 15:39
memberRavi LVS10-Nov-11 15:39 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 11 Nov 2011
Article Copyright 2011 by George Swan
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid