Results 1 to 5 of 5

Thread: Calc difference between 2 dates but excluding weekends!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    S.A
    Posts
    89

    Question Calc difference between 2 dates but excluding weekends!

    Hi everyone

    I want to calculate the difference (in days) between 2 dates (user will select them from a calendar). But it should exclude weekends (Saterday & Sunday).....

    Thanks
    I forgot my password....

  2. #2
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Hey, kid. Where's your best body Butch Cassidy, ha?
    ;-)
    Anyway, you may use DateDiff function and substract 2 from the result.
    McGenius

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    McGenius, maybe you should reconsider your name here.
    You can't just substract 2, because you must figure out the number of weekends first.

    Here is a function i used before. I believe i stole half of it from this forum

    VB Code:
    1. Public Function NettoWorkdays(ByVal dtmStart As Date, ByVal dtmEnd As Date) As Integer
    2.  
    3. 'This function calculates the number of working days (monday to friday) between 2 dates,
    4. 'including the first and the last day
    5.    
    6.     Dim intDays As Integer
    7.     Dim intSubtract As Integer
    8.    
    9.     ' if end is smaller then start return -1
    10.     If dtmEnd < dtmStart Then
    11.         NettoWorkdays = -1
    12.     Else
    13.         ' Get the start and end dates to be weekdays.
    14.         While WeekDay(dtmStart) = vbSaturday Or WeekDay(dtmStart) = vbSunday
    15.             dtmStart = dtmStart + 1
    16.         Wend
    17.         While WeekDay(dtmEnd) = vbSaturday Or WeekDay(dtmEnd) = vbSunday
    18.             dtmEnd = dtmEnd - 1
    19.         Wend
    20.         If dtmStart > dtmEnd Then
    21.             ' Sorry, no Workdays to be had. Just return 0.
    22.             NettoWorkdays = 0
    23.         Else
    24.             intDays = dtmEnd - dtmStart + 1
    25.            
    26.             ' Subtract off weekend days.  Do this by figuring out how
    27.             ' many calendar weeks there are between the dates, and
    28.             ' multiplying the difference by two (because there are two
    29.             ' weekend days for each week). That is, if the difference
    30.             ' is 0, the two days are in the same week. If the
    31.             ' difference is 1, then we have two weekend days.
    32.             intSubtract = (DateDiff("ww", dtmStart, dtmEnd) * 2)
    33.            
    34.             NettoWorkdays = intDays - intSubtract
    35.         End If
    36.     End If
    37.    
    38. End Function
    Frans

  4. #4
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Originally posted by Frans C
    McGenius, maybe you should reconsider your name here.
    You can't just substract 2, because you must figure out the number of weekends first ...[/Highlight]
    Hey, you dummy, you better think before sending this s**** to me. If you're a PROGRAMMER then all need is an idea - the rest is upto capability if your brain and willingness of doing new things.
    McGenius

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    S.A
    Posts
    89
    Thanks for the replies dudes

    McGenuis : Thanks ... my best bod cassidy is @home (I got 2 , the wife and the basset hound).... and the Basset is probably still pi$$ed because I she took all the bed covers last night, and when I moved her she growled like a lion


    Frans C: Thanks for the code... I needed it quickly so I didn't have time to figure out how to do it
    I forgot my password....

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