Solved

Add working days to date

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
edit .asp files 5 31
run a stored procedure from vb.net 1 27
VB.net Adding a row to a DataGridView that has a checkbox column 5 23
IDE for Python 5 21
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

837 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