Solved

Add working days to date

Posted on 2008-10-22
2
497 Views
Last Modified: 2013-11-07
Hello,

I'm new to ASP. I'm trying to make a function to add working days to a date.

In other words, I have, say, StartDate="22-10-2008" and NrDays=5. I would like to add 5 working days (23,24,25,28 and 29) and return EndDate="29-10-2008".

Can I make a function to do that?

Thanks in advance,

jppinto
0
Comment
Question by:jppinto
2 Comments
 
LVL 13

Accepted Solution

by:
crazyman earned 250 total points
ID: 22775707
0
 
LVL 19

Assisted Solution

by:Stephen Manderson
Stephen Manderson earned 250 total points
ID: 22775808
Hi there

The following function will do what your after.

Example usage..
'AddBusinessDays(DateTime.Now, 5)
'returns 29/10/2008 13:32:27

Regards
Steve
        Public Shared Function AddBusinessDays(ByVal AddToDate As DateTime, ByVal BusinessDaysToAdd As Integer) As DateTime
            If BusinessDaysToAdd = 0 Then Return AddToDate
            If BusinessDaysToAdd > 0 Then
                Dim TotalDaysCount As Integer = 0
                Dim BusinessDaysCount As Integer = 0
 
                Dim ReturnDate As DateTime
                While BusinessDaysCount < BusinessDaysToAdd
                    TotalDaysCount += 1
                    ReturnDate = AddToDate.AddDays(TotalDaysCount)
                    If Not (ReturnDate.DayOfWeek = DayOfWeek.Saturday Or ReturnDate.DayOfWeek = DayOfWeek.Sunday) Then
                        BusinessDaysCount += 1
                    End If
                End While
 
                Return ReturnDate
            Else
                Throw New ArgumentException("Negative amount")
            End If
        End Function

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question