count days including weekend

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike

    count days including weekend

    I have a routine that's calculating business days but its not counting the
    weekend days that are between the start date and end date. If my start date
    is 9/26/08 and my end date is 10/01/08, I should see 4 business days and 2
    weekend days.

    How can I get that result? I'm getting 4 business days but its not counting
    the weekend days?


  • CSharper

    #2
    Re: count days including weekend

    On Sep 26, 8:31 am, "Mike" <whyyoulookinga ...@gmail.comwr ote:
    I have a routine that's calculating business days but its not counting the
    weekend days that are between the start date and end date. If my start date
    is 9/26/08 and my end date is 10/01/08, I should see 4 business days and 2
    weekend days.
    >
    How can I get that result? I'm getting 4 business days but its not counting
    the weekend days?
    here is a simple code I wrote

    static void Main(string[] args)
    {
    DateTime st = DateTime.Parse( "01/01/2008");
    DateTime et = DateTime.Parse( "01/10/2008");
    DateTime dt = st;
    int weekend =0;
    int weekdays = 0;
    while (dt < et)
    {
    Console.WriteLi ne("Day of the week {0}",
    dt.DayOfWeek);
    int temp = ((dt.DayOfWeek == DayOfWeek.Satur day) ||
    (dt.DayOfWeek == DayOfWeek.Sunda y)) ? weekend++ : weekdays++;
    dt = dt.AddDays(1);
    }
    Console.Read();
    }

    Comment

    • Mike

      #3
      Re: count days including weekend

      I tried your code snippet and its not working, If I enter in "09/26/2008"
      and "10/01/2008" it returns 1 weekend day, it should be 2.

      "CSharper" <csharper@gmx.c omwrote in message
      news:36de38ef-8a8b-4905-9385-69585d08f879@f6 3g2000hsf.googl egroups.com...
      On Sep 26, 8:31 am, "Mike" <whyyoulookinga ...@gmail.comwr ote:
      I have a routine that's calculating business days but its not counting the
      weekend days that are between the start date and end date. If my start
      date
      is 9/26/08 and my end date is 10/01/08, I should see 4 business days and 2
      weekend days.
      >
      How can I get that result? I'm getting 4 business days but its not
      counting
      the weekend days?
      here is a simple code I wrote

      static void Main(string[] args)
      {
      DateTime st = DateTime.Parse( "01/01/2008");
      DateTime et = DateTime.Parse( "01/10/2008");
      DateTime dt = st;
      int weekend =0;
      int weekdays = 0;
      while (dt < et)
      {
      Console.WriteLi ne("Day of the week {0}",
      dt.DayOfWeek);
      int temp = ((dt.DayOfWeek == DayOfWeek.Satur day) ||
      (dt.DayOfWeek == DayOfWeek.Sunda y)) ? weekend++ : weekdays++;
      dt = dt.AddDays(1);
      }
      Console.Read();
      }


      Comment

      • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

        #4
        RE: count days including weekend

        Can you post your code?

        "Mike" wrote:
        I have a routine that's calculating business days but its not counting the
        weekend days that are between the start date and end date. If my start date
        is 9/26/08 and my end date is 10/01/08, I should see 4 business days and 2
        weekend days.
        >
        How can I get that result? I'm getting 4 business days but its not counting
        the weekend days?
        >
        >
        >

        Comment

        • Mike

          #5
          Re: count days including weekend

          I'm trying this which I found online some time back:
          public void AddWeekdays(Dat eTime start, int days)
          {
          try
          {
          if (start.DayOfWee k == DayOfWeek.Satur day || start.DayOfWeek ==
          DayOfWeek.Sunda y)
          {

          }
          else
          {
          int remainder = days % 5;
          int weekend = (5 / days) *2;

          DateTime end = start.AddDays(r emainder);
          if (end.DayOfWeek == DayOfWeek.Satur day)
          {
          end = end.AddDays(2);
          }
          else if (end.DayOfWeek < start.DayOfWeek )
          {
          end = end.AddDays(2);
          }
          txtWDays.Value = weekend.ToStrin g();
          txtCDates.Text = end.AddDays(day s + weekend -
          remainder).ToSh ortDateString() ;
          }

          }
          catch (Exception ex)
          {
          }
          }

          "Family Tree Mike" <FamilyTreeMike @discussions.mi crosoft.comwrot e in
          message news:439373CA-3F31-40D1-BFF2-F31D76D3E5E7@mi crosoft.com...
          Can you post your code?
          >
          "Mike" wrote:
          >
          >I have a routine that's calculating business days but its not counting
          >the
          >weekend days that are between the start date and end date. If my start
          >date
          >is 9/26/08 and my end date is 10/01/08, I should see 4 business days and
          >2
          >weekend days.
          >>
          >How can I get that result? I'm getting 4 business days but its not
          >counting
          >the weekend days?
          >>
          >>
          >>

          Comment

          • CSharper

            #6
            Re: count days including weekend

            On Sep 26, 9:26 am, Family Tree Mike
            <FamilyTreeM... @discussions.mi crosoft.comwrot e:
            Can you post your code?
            >
            "Mike" wrote:
            I have a routine that's calculating business days but its not counting the
            weekend days that are between the start date and end date. If my start date
            is 9/26/08 and my end date is 10/01/08, I should see 4 business days and 2
            weekend days.
            >
            How can I get that result? I'm getting 4 business days but its not counting
            the weekend days?
            When I ran the same code changing the date to what you specified I got
            3 weekdays and 2 weekends. This is what I changed

            static void Main(string[] args)
            {
            DateTime st = DateTime.Parse( "09/26/2008");
            DateTime et = DateTime.Parse( "10/01/2008");
            DateTime dt = st;
            int weekend =0;
            int weekdays = 0;
            while (dt < et)
            {
            Console.WriteLi ne("Day of the week {0}",
            dt.DayOfWeek);
            int temp = ((dt.DayOfWeek == DayOfWeek.Satur day) ||
            (dt.DayOfWeek == DayOfWeek.Sunda y)) ? weekend++ : weekdays++;
            dt = dt.AddDays(1);
            }
            Console.WriteLi ne("Weekdays {0} and Weekends {1}",
            weekdays, weekend);
            Console.Read();
            }

            Comment

            • CSharper

              #7
              Re: count days including weekend

              On Sep 26, 9:32 am, "Mike" <whyyoulookinga ...@gmail.comwr ote:
              I'm trying this which I found online some time back:
              public void AddWeekdays(Dat eTime start, int days)
                  {
                      try
                      {
                          if (start.DayOfWee k == DayOfWeek.Satur day || start.DayOfWeek ==
              DayOfWeek.Sunda y)
                          {
              >
                         }
                         else
                          {
                              int remainder = days % 5;
                              int weekend = (5 / days) *2;
              >
                              DateTime end = start.AddDays(r emainder);
                              if (end.DayOfWeek == DayOfWeek.Satur day)
                              {
                                  end = end.AddDays(2);
                              }
                              else if (end.DayOfWeek < start.DayOfWeek )
                              {
                                  end = end.AddDays(2);
                              }
                              txtWDays.Value = weekend.ToStrin g();
                              txtCDates.Text = end.AddDays(day s + weekend -
              remainder).ToSh ortDateString() ;
                          }
              >
                      }
                      catch (Exception ex)
                      {
                      }
                  }
              >
              "Family Tree Mike" <FamilyTreeM... @discussions.mi crosoft.comwrot e in
              messagenews:439 373CA-3F31-40D1-BFF2-F31D76D3E5E7@mi crosoft.com...
              >
              Can you post your code?
              >
              "Mike" wrote:
              >
              I have a routine that's calculating business days but its not counting
              the
              weekend days that are between the start date and end date. If my start
              date
              is 9/26/08 and my end date is 10/01/08, I should see 4 business days and
              2
              weekend days.
              >
              How can I get that result? I'm getting 4 business days but its not
              counting
              the weekend days?
              If you still want to use your code logic, you need change the code
              like the following

              public void AddWeekdays(Dat eTime start, int days)
              {
              try
              {
              if (start.DayOfWee k == DayOfWeek.Satur day ||
              start.DayOfWeek ==
              DayOfWeek.Sunda y)
              {
              }
              else
              {
              int remainder = days % 5;
              int weekend = ( days / 5) *2; // change here
              DateTime end = start.AddDays(r emainder);
              if (end.DayOfWeek == DayOfWeek.Satur day)
              {
              end = end.AddDays(2);
              }
              else if (end.DayOfWeek < start.DayOfWeek )
              {
              end = end.AddDays(2);
              }
              txtWDays.Value = weekend.ToStrin g();
              txtCDates.Text = end.AddDays(day s - weekend -
              remainder).ToSh ortDateString() ; //change here
              }
              }
              catch (Exception ex)
              {
              }
              }

              Try this code and let me know if it works.

              Comment

              • Ignacio Machin ( .NET/ C# MVP )

                #8
                Re: count days including weekend

                On Sep 26, 10:23 am, "Mike" <whyyoulookinga ...@gmail.comwr ote:
                I tried your code snippet and its not working, If I enter in "09/26/2008"
                and "10/01/2008" it returns 1 weekend day, it should be 2.
                >
                "CSharper" <cshar...@gmx.c omwrote in message
                >
                news:36de38ef-8a8b-4905-9385-69585d08f879@f6 3g2000hsf.googl egroups.com...
                On Sep 26, 8:31 am, "Mike" <whyyoulookinga ...@gmail.comwr ote:
                >
                I have a routine that's calculating business days but its not counting the
                weekend days that are between the start date and end date. If my start
                date
                is 9/26/08 and my end date is 10/01/08, I should see 4 business days and 2
                weekend days.
                >
                How can I get that result? I'm getting 4 business days but its not
                counting
                the weekend days?
                >
                here is a simple code I wrote
                >
                static void Main(string[] args)
                {
                DateTime st = DateTime.Parse( "01/01/2008");
                DateTime et = DateTime.Parse( "01/10/2008");
                DateTime dt = st;
                int weekend =0;
                int weekdays = 0;
                while (dt < et)
                {
                Console.WriteLi ne("Day of the week {0}",
                dt.DayOfWeek);
                int temp = ((dt.DayOfWeek == DayOfWeek.Satur day) ||
                (dt.DayOfWeek == DayOfWeek.Sunda y)) ? weekend++ : weekdays++;
                dt = dt.AddDays(1);
                }
                Console.Read();
                }
                I think that you would have to iterate.
                I have a req. to count the business days in a month.This is the code
                I'm using:
                static int BusinessDaysInM onth(DateTime dt) {
                DateTime startDate = dt.AddDays(1 - dt.Day); //make
                sure we are on day 1 of the month
                int days = 1;
                while (startDate.Mont h == dt.Month) {
                if ((startDate.Day OfWeek != DayOfWeek.Satur day) &&
                (startDate.DayO fWeek != DayOfWeek.Sunda y))
                days++;
                startDate = startDate.AddDa ys(1);
                }
                return days;
                }

                Comment

                Working...