Solved

Add working days to date

Posted on 2008-10-22
2
495 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 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