Results 1 to 9 of 9

Thread: Number of weekend days?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    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++;
            }

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Number of weekend days?

    ill give that a shot, thanks!

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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)

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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()));
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width