|
-
May 1st, 2013, 10:22 AM
#1
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.
-
May 1st, 2013, 10:26 AM
#2
New Member
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
-
May 1st, 2013, 12:40 PM
#3
Re: Adding working days to date
 Originally Posted by DavidAllanJames
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!
-
May 1st, 2013, 02:54 PM
#4
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 -
-
May 1st, 2013, 02:59 PM
#5
Re: Adding working days to date
 Originally Posted by stanav
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|