Solved

Add working days to date

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now