|
-
Aug 4th, 2006, 12:11 AM
#1
Thread Starter
PowerPoster
Number of weekend days?
I have 2 dates (Start/end)
I want to find out the number of weekends (or rather, total number of weekend days) that falls into those 2 dates.
how can I do this?
-
Aug 4th, 2006, 04:04 AM
#2
Re: Number of weekend days?
How big a date range might be entered?
You could loop through every single date in the range and add to a counter if it's a weekday. But if it's a huge range it might take a while.
Code:
if(d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday)
{
intWeekDayCounter++;
}
-
Aug 4th, 2006, 04:27 AM
#3
Re: Number of weekend days?
Here's a start:
Code:
DateTime startDate = DateTime.MinValue;
DateTime endDate = DateTime.MaxValue;
int dayCount;
int weekCount = Math.DivRem(endDate.Subtract(startDate).Days, 7, out dayCount);
The final answer will then be 2*weekCount plus however many weekend days are in dayCount, which is the remainder. There may well be a formula you could use for that based on the number of days and the weekly day number of the first day in that remainder, which will be the same as that for startDate, but if there is I couldn't be bothered working it out. Given that that remainder can be no more than six it's easy enough to check each day it contains individually.
-
Aug 4th, 2006, 05:07 PM
#4
Thread Starter
PowerPoster
Re: Number of weekend days?
ill give that a shot, thanks!
-
Aug 4th, 2006, 05:34 PM
#5
Thread Starter
PowerPoster
Re: Number of weekend days?
hmm that didnt quite work, probably something im doing wrong I'm sure.
if say there were 5 days total of weekends (over 3 weeks) it would return me 8 (start from 8/8/2006 -> 26/8/2006)
answer in this case should be 5
int dayCount
int weekCount = Math.DivRem(endDate.Subtract(startDate).Days, 7, out dayCount);
return 2 * weekCount + dayCount
Last edited by Techno; Aug 4th, 2006 at 10:07 PM.
-
Aug 4th, 2006, 10:46 PM
#6
Thread Starter
PowerPoster
Re: Number of weekend days?
should it not be something like:
Code:
int numberOfDaysInWeek = 7;
int numberOfDaysInWeekend = 2;
int totalWeekendDays = totalDaysTimeSpan * numberOfDaysInWeekend / numberOfDaysInWeek;
where totalDaysTimeSpan is the endDate - startDate
don't think that'll work as you could be staying for say 4 days, (Thursday - Monday) and it would calculate that as 1, where it should be 2 (sat + sun)
-
Aug 5th, 2006, 04:09 AM
#7
Re: Number of weekend days?
I didn't say that it was (2 * weekCount + dayCount). I said that it was (2 * weekCount + X) where X is the number of weekend days in dayCount. If you cannot arrive at a formula for that based on the index of the first day and the number of days then you could just check each day to see if it's Saturday or Sunday. It's easy enough to do that because you know what day of the week the first day is because it's the same as startDate.
-
Aug 5th, 2006, 03:44 PM
#8
Thread Starter
PowerPoster
Re: Number of weekend days?
hmmm sorry, really slow. still not following you on the X - where X is the number of weekend days in dayCount - but dayCount is returned back from the Math.DivRem.
-
Aug 5th, 2006, 06:58 PM
#9
Re: Number of weekend days?
Code:
// This is the first day in the period.
DateTime startDate = DateTime.MinValue;
// This is the last day in the period.
DateTime endDate = DateTime.MaxValue;
// This is the number of days left in the period after removing the whole weeks.
int dayCount;
// This is the number of whole weeks in the period.
int weekCount = Math.DivRem(endDate.Subtract(startDate).Days, 7, out dayCount);
// Each whole week has two weekend days in it.
int weekendDayCount = 2 * weekCount;
// The days left over start on the same day of the week as the period itself.
DateTime dayDate = startDate;
// Test the remaining days to see if they are weekend days.
for (int offset = 0; offset < dayCount; offset++)
{
switch (dayDate.AddDays(offset).DayOfWeek)
{
case DayOfWeek.Saturday:
case DayOfWeek.Sunday:
weekendDayCount++;
break;
default:
break;
}
}
MessageBox.Show(string.Format("There are {0} weekend days from {1} to {2}.",
weekendDayCount,
startDate.ToShortDateString(),
endDate.ToShortDateString()));
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|