Results 1 to 18 of 18

Thread: Adding working days to date

Hybrid View

  1. #1
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Adding working days to date

    Just another link but it looks promising:

    http://www.codeproject.com/Articles/...Days-to-a-Date

    I just tested it and it seems to work OK. Very similar to what is already posted.

    Code:
    Public Function CalculateTenBusinessDaysFromInputDate(ByVal StartDate As Date) As Date
            'This simply adds at least 2 full weeks to the start date.
    
            Select Case StartDate.DayOfWeek
    
                Case DayOfWeek.Sunday
                    'if the start date is not a sunday you need to add 
                    '1 day to push it to a monday that is why the number is 15.
                    Return StartDate.AddDays(15)
                Case DayOfWeek.Monday, DayOfWeek.Tuesday, _
                DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday
                    'if the start date is any other day then just add 14 days to the start date.
                    Return StartDate.AddDays(14)
                Case DayOfWeek.Saturday
                    'if the start date is on a Saturday you need to add 
                    '2 days to push it to a monday that is why the number is 16.
                    Return StartDate.AddDays(16)
                Case Else
                    Return StartDate
    
            End Select
    
        End Function
    Last edited by TysonLPrice; May 1st, 2013 at 10:30 AM.

  2. #2
    New Member
    Join Date
    Apr 2013
    Posts
    6

    Re: Adding working days to date

    Yep... For...Next Loop... Keep trying guys; you are not going to find a faster more eligant solution than basically a 1 liner:
    CalcDueDate = DateAdd("d", vAgreedTRT Mod 5, _
    DateAdd("ww", Int(vAgreedTRT / 5), vDateReceived))
    But keep trying... You may get there!
    x

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Adding working days to date

    Quote Originally Posted by DavidAllanJames View Post
    Yep... For...Next Loop... Keep trying guys; you are not going to find a faster more eligant solution than basically a 1 liner:
    CalcDueDate = DateAdd("d", vAgreedTRT Mod 5, _
    DateAdd("ww", Int(vAgreedTRT / 5), vDateReceived))
    But keep trying... You may get there!
    x
    Does this work? If vAgreedTRT is the number of days, and vDateReceived is the date we are starting with it doesn't!
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Adding working days to date

    That is only part of his code... The full code is in post#5, which in no way resembles a single line.... Does it work? I don't know... but I take his words that it does.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Adding working days to date

    Quote Originally Posted by stanav View Post
    That is only part of his code... The full code is in post#5, which in no way resembles a single line.... Does it work? I don't know... but I take his words that it does.
    I guess it was the reference to it being a 'one-liner' that got me.

    My one-liner

    Code:
        Private Function addWorkingDays(d As DateTime, daysToAdd As Integer) As DateTime
            If daysToAdd <= 0 Then Throw New Exception("foo")
            Dim rv As DateTime = d
            Dim ctDays As Integer = daysToAdd
            'force date to a Monday
            'note: a Sat or Sun start date + 1 day = a Monday
            Do While rv.DayOfWeek <> DayOfWeek.Monday AndAlso ctDays > 0
                rv = rv.AddDays(1)
                ctDays -= 1
                If rv.DayOfWeek = DayOfWeek.Saturday Then
                    rv = rv.AddDays(2)
                ElseIf rv.DayOfWeek = DayOfWeek.Sunday Then
                    rv = rv.AddDays(1)
                End If
            Loop
            Dim wk As Integer = ctDays \ 5 'how many business weeks
            ctDays -= wk * 5 'substract from day count
            ctDays += wk * 7 'convert business weeks to calendar days
            rv = rv.AddDays(ctDays)
            Return rv
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

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