Introduction
In many cases developers have a demand of calculation and manipulations with business dates.
Scenario:
We want to add “N” business days/working days to a current date (Or from a specified date). Where “N” is the number of business days.
Solution:
In this blog I have described how to add “N” business days/working days to a date.
The below C# function does it all for you. Simply pass in a date, along with the number of working days you want to add.
And this's how you call it in your application:
Public Function AddBusinessDays(ByVal DateIn As DateTime, ByVal Days2Add As Integer) As DateTime
Dim resultDate As DateTime
' Adds the [Days2Add] number of working days to DateIn
resultDate = DateIn.AddDays(Days2Add)
' Loops and adds only non-weekend day(working days)
While Weekday(resultDate) = 1 Or Weekday(resultDate) = 7
resultDate = resultDate.AddDays(IIf(Days2Add < 0, -1, 1))
End While
Return resultDate
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newDate As DateTime = AddBusinessDays(Today, 5)
MsgBox("The 5th Business day from today is " & newDate)
End Sub
The above code is tested.Dim newDate As DateTime = AddBusinessDays(Today, 5)
MsgBox("The 5th Business day from today is " & newDate)
End Sub
No comments:
Post a Comment