Solved

Add working days to date

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

939 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

10 Experts available now in Live!

Get 1:1 Help Now