Dim businessDaysToAdd As Integer = 8
Dim addedDays As Integer = 0
While addedDays < businessDaysToAdd
initialDate = initialDate.AddDays(1)
If initialDate.DayOfWeek <> DayOfWeek.Saturday AndAlso initialDate.DayOfWeek <> DayOfWeek.Sunday Then
addedDays += 1
End If
End While
resultDate = initialDate
Input Arguments - initialDate (DateTime) - The starting date.
Output Arguments - resultDate`(DateTime) - The resulting date after adding 8 business days.
I know that there are many ways to do this by performing logic and arithmetic calculations. But, are you considering holidays? I worked on a project with a similar requirement and I used the BusinessDaysCalculator activity:
Just install it, select the country of preference, and calculate the days as needed. No complex calculations.
This is the code that I created to add 10 business days to a DateTime variable.
To add 8 business working days to a date while excluding weekends in UiPath, you can use the following approach:
Assign Activity*: Create a variable to hold the initial date.
While Loop*: Use a While loop to iterate through each day, adding a day at a time.
If Activity*: Inside the loop, use an If activity to check if the current day is not Saturday or Sunday.
Add Business Days*: If it’s a business day, increment a counter.
Break Condition*: Exit the loop when the counter reaches 8.
Here’s a workflow code for the above steps:
startDate = new DateTime(2023, 2, 3) // Replace with your start date
businessDaysToAdd = 8
addedDays = 0
While addedDays < businessDaysToAdd
startDate = startDate.AddDays(1)
If startDate.DayOfWeek <> DayOfWeek.Saturday AndAlso startDate.DayOfWeek <> DayOfWeek.Sunday Then
addedDays += 1
End If
End While
// Output startDate will have the date after adding 8 business days