TimeSpan between Business Days

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pkj7461
    New Member
    • Oct 2007
    • 15

    TimeSpan between Business Days

    Hi,
    I was looking for the code to return Timespan between any two days. The days should exclude weekends, saturday and sunday.
    Like, for example,
    startdate = "10/5/2007 10:00:00 AM"
    enddate = "10/9/2007 11:30:00 AM"

    The function should return the Timespan between these two days excluding Saturday and Sunday...
    Timespan ts = 1:12:30:00

    Thanks in Advance,
    PKJ
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Originally posted by pkj7461
    Hi,
    I was looking for the code to return Timespan between any two days. The days should exclude weekends, saturday and sunday.
    Like, for example,
    startdate = "10/5/2007 10:00:00 AM"
    enddate = "10/9/2007 11:30:00 AM"

    The function should return the Timespan between these two days excluding Saturday and Sunday...
    Timespan ts = 1:12:30:00

    Thanks in Advance,
    PKJ
    [CODE=cpp]DateTime startDate = DateTime.Now;
    DateTime endDate = DateTime.Now.Ad dDays(20);
    TimeSpan ts = endDate.Subtrac t(startDate);
    Console.WriteLi ne(ts.ToString( ));
    int weekends = 0;
    for (DateTime tempDt = startDate; tempDt < endDate; tempDt = tempDt.AddDays( 1))
    {
    if (tempDt.DayOfWe ek == DayOfWeek.Satur day || tempDt.DayOfWee k == DayOfWeek.Sunda y)
    weekends++;
    }
    ts = ts.Subtract(new TimeSpan(weeken ds, 0, 0, 0));
    Console.WriteLi ne(ts.ToString( ));[/CODE]

    does this help you?

    cheers

    Comment

    • pkj7461
      New Member
      • Oct 2007
      • 15

      #3
      Hi,
      Sorry for delay in replying.
      This seems to be not working. It gives the timespan including weekends.
      Thanks,
      Prasanna






      Originally posted by Shashi Sadasivan
      [CODE=cpp]DateTime startDate = DateTime.Now;
      DateTime endDate = DateTime.Now.Ad dDays(20);
      TimeSpan ts = endDate.Subtrac t(startDate);
      Console.WriteLi ne(ts.ToString( ));
      int weekends = 0;
      for (DateTime tempDt = startDate; tempDt < endDate; tempDt = tempDt.AddDays( 1))
      {
      if (tempDt.DayOfWe ek == DayOfWeek.Satur day || tempDt.DayOfWee k == DayOfWeek.Sunda y)
      weekends++;
      }
      ts = ts.Subtract(new TimeSpan(weeken ds, 0, 0, 0));
      Console.WriteLi ne(ts.ToString( ));[/CODE]

      does this help you?

      cheers

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        Could you please give some input dates (start and end).

        I tested it, and it was working allright.
        Once you send some test data.....I can check it again.

        Have you in anyways modified the code anywhere?

        cheers

        Comment

        • pkj7461
          New Member
          • Oct 2007
          • 15

          #5
          Hi Shashi,
          I was working on weekend with the code. I changed the input to
          Start Date = "10/4/2007 3:26:00 PM"
          endDate = " 10/7/2007 9:24:00 PM"

          I ran the code and changed the format of output to "hh:mm"
          My output was 29:58
          and the actual output should be 32:33.
          I noticed one more thing. The output seems to be changing every minute even during weekend.
          Thank you very much for your time and efforts.
          Prasanna.



          Originally posted by Shashi Sadasivan
          Could you please give some input dates (start and end).

          I tested it, and it was working allright.
          Once you send some test data.....I can check it again.

          Have you in anyways modified the code anywhere?

          cheers

          Comment

          • Shashi Sadasivan
            Recognized Expert Top Contributor
            • Aug 2007
            • 1435

            #6
            Checking your specifications :
            you wanted the timespan between business days :)
            But in your first post you wanted between any 2 days.

            I went along with the title "between 2 business days"
            (why do you want staff to work on weekends :P)
            7th of 10 is not a business day.

            so now you know where to modify the code.

            cheers
            Last edited by Shashi Sadasivan; Oct 8 '07, 01:42 AM. Reason: explaingin on what went wrong !

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Well DateTime objects have that "Day of the week" property.
              You can create DateTime objects for each whole "day" inbetween your two dates. If the dayoftheweek is not a weekend day, add 24?
              That's a really simplified reasoning on it, but then you just have to find the "used" time on the start/end day, assuming they are "week days"

              Comment

              Working...