Calculate Business Days Solution (+/- days)

Hey all,

I have developed a solution for adding or subtracting business days. I posted the code below and with a simple if statement you should also be able to weed out holidays as well. Enjoy

Sub Click(Source As Button)

Dim workspace As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Set uidoc = workspace.CurrentDocument

Dim num As Integer

Dim totaldays As Integer

Dim newtotal As Integer

Dim dateTime As NotesDateTime

Dim temp As String

Dim dateTime2 As Variant

Dim dateTime3 As NotesDateTime

Dim dateTime4 As Variant

Dim x As Variant, wd As Integer

Dim counter1 As Integer

Dim counter2 As Integer



' Calculates Business days based on a start date and a given number of business days.

' To move a date back you must enter a negative number in the field (days) 

Set dateTime = New NotesDateTime(Cdat(uidoc.FieldGetText("date")))	

dateTime2 = dateTime.DateOnly

Call uidoc.FieldSetText("date2",datetime2)





Do Until counter1 = Cint(uidoc.FieldGetText("days"))

	wd = Weekday(dateTime.DateOnly)

	If wd > 0 Then 

		If Cint(uidoc.FieldGetText("days")) > 0 Then

			counter2 = counter2 + 1

		Else

			counter2 = counter2 - 1

		End If

’ counter2 = counter2 + 1

		If wd = 2 Or wd = 3 Or wd =4 Or wd =5 Or wd = 6 Then

			If Cint(uidoc.FieldGetText("days")) > 0 Then

				counter1 = counter1 + 1

			Else

				counter1 = counter1 - 1

			End If

			

		End If

	End If

	If Cint(uidoc.FieldGetText("days")) > 0 Then

		Call dateTime.AdjustDay(1)	

	Else

		Call dateTime.AdjustDay(-1)

	End If

Loop 

Set dateTime3 = New NotesDateTime(Cdat(uidoc.FieldGetText("date2")))

Call dateTime3.AdjustDay(counter2)

wd = Weekday(dateTime3.DateOnly)

If wd = 7 Then

	If Cint(uidoc.FieldGetText("days")) > 0 Then

		Call dateTime3.AdjustDay(2)

	Else

		Call dateTime3.AdjustDay(-1)

	End If

Elseif wd = 1 Then

	If Cint(uidoc.FieldGetText("days")) > 0 Then

		Call dateTime3.AdjustDay(1)

	Else

		Call dateTime3.AdjustDay(-2)

	End If

Else

	'do nothing

End If

dateTime4 = dateTime3.DateOnly

Call uidoc.FieldSetText("newdate",datetime4)

End Sub

Subject: Calculate Business Days Solution (+/- days)

You should post this in the sandbox as well.

Subject: Calculate Business Days Solution (+/- days)

Adam,Good post, but it does look a little long (codewise). I do have to ask what happens if the start date is the not a business day (like Saturday or Sunday).

I thought you wanted to find the future (or past) business day having a start date that is a valid business day.

just my 2 cents.

Subject: RE: Calculate Business Days Solution (+/- days)

Well, that would be fine if it were a Saturday or Sunday. But the inital start date could be set to “Today” which would be a valid business if they worked monday - friday… Although it techically should not matter simply because this code calculates a future business day not allowing to end on a Sat. or Sun… I think some of the others responding misinterpreted the use of the code. This will take a specific date (weekday or weekend) and add (x) number of business days to equal a future business date…

Example.

I used it to calculate a future audit date.

Subject: that’s a lot of coding, my way is bullet proof

In lotus notes, i created a database that starts with 1/1/02 (tue) and has a value of 1.

then the next record is

1/2/02 Wed 2,

1/3/02 Thu 3,

1/4/02 Fri 4,

1/5/02 Sat 5,

1/6/02 Sun 5,

1/7/02 Mon 5

and so on to 1/1/09 (used foxpro to create)

if start date is 1/5/02 and end date is 1/7/02

then 5-5=0 days

if start date is 1/2/02 end date 1/7/02,then

5-2=3 days

it’s working bullet proof, enter a start date of 1/1/02 and enddate of 1/1/05, in 1 sec, it tells you the diff & it’s accurate. I used the formula language and it messed up after a large number of days.

Subject: RE: that’s a lot of coding, my way is bullet proof

Then again, there is @BusinessDays:

@BusinessDays(StartDate; EndDate;

@TextToNumber(NonWorkDays);

Holidays)

Why script what formula does easier?:slight_smile:

Subject: @BusinessDay is not really a good thing to use here

Technically @BusinessDays only give the number of BusinessDays between 2 dates. What the gentleman who shares the LS was trying to do is find the future (or past) business day.

@Businessday does not work in this circumstance because if the start date and end date are the same date, it returns one when it should return 0.

I have some code I wrote in Formula which work… I am going to post it on my blog tonight (08/31/2004: the code and website example is now available).

HTH

http://www.dangsite.com/blog

Subject: RE: @BusinessDay is not really a good thing to use here

Hey Joe,

I checked blog and tested your formula… Works great.

Adam

Subject: RE: @BusinessDay is not really a good thing to use here

True, it all depends on what exaxtly you are trying to accomplish.

The hard coded years suggested above is not good either.

Here is code I use to validate that a day requested is indeed a work day and not a weekend or holiday:

“Holidays” is a cfd hidden field with code that is specific for the application. You could also do a Holiday lookup to names.nsf.

Workweek is M-F

Sub Onchange(Source As Field)

Dim ws1 As New NotesUIWorkspace

Dim uidoc1 As NotesUIDocument

Set uidoc1=ws1.currentdocument

Dim doc1 As NotesDocument

Set doc1=uidoc1.Document

Dim strDateNeeded As String

strdateNeeded=uidoc1.FieldGetText(“eqcodateNeeded”)

Dim wd As Integer

wd=Weekday(Cdat(strDateNeeded))

Dim strOk As String

Select Case wd

Case 1: strOK=“No”

Case 7: strOK=“No”

Case Else: strOK=“Yes”

End Select

Dim itemHolidays As NotesItem

Set itemHolidays=doc1.GetFirstItem(“Holidays”)

If itemHolidays.contains(Cstr(strDateNeeded))

Then

strOK=“NoHoliday”

End If

If strOK=“Yes” Then

Call uidoc1.fieldsettext

("eqcoCheckIndate",strdateNeeded)

Call uidoc1.gotofield(“eqcoStartTime”)

Elseif strOK=“NoHoliday” Then

Messagebox "You cannot select a holiday for

checkout. Please select another date."

doc1.eqcoDateNeeded=“”

Call uidoc1.gotofield(“eqcoDateNeeded”)

Else

Messagebox "You must select a weekday for

checkout. Please select another date."

doc1.eqcoDateNeeded=“”

Call uidoc1.gotofield(“eqcoDateNeeded”)

End If

End Sub

Subject: what?

why do you have Sat, Sun, and Mon as the name number?

I don’t understand why this better:

  1. it is FoxPro and LDD isn’t a FoxPro forum.

  2. your way to solve the problem is not really a programatic solution. What you have is a big pseudo cheat sheet? I could easily “break” the system by giving you a date that was before 1/1/02.

I can not see what is better in your way.

Subject: in our case (time off request)

it works ok. No one submits time off before 2001, that is why i have a large range of numbers until 2009. It might be a cheat sheet, but it works. is @businessdays a new function?

Subject: RE: in our case (time off request)

You really can not make a comparsion to what I was commenting on to your solution because they are dissimilar. One is true calculated logic and the other is just a big massive cheat sheet of dates from 2001 to 2009.