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?
Printable View
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?
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++;
}
Here's a start: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.Code:DateTime startDate = DateTime.MinValue;
DateTime endDate = DateTime.MaxValue;
int dayCount;
int weekCount = Math.DivRem(endDate.Subtract(startDate).Days, 7, out dayCount);
ill give that a shot, thanks!
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
should it not be something like:
where totalDaysTimeSpan is the endDate - startDateCode:
int numberOfDaysInWeek = 7;
int numberOfDaysInWeekend = 2;
int totalWeekendDays = totalDaysTimeSpan * numberOfDaysInWeekend / numberOfDaysInWeek;
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)
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.
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.
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()));