Calculating the Next Working Day in .NET

Sometimes you don't just want to add a certain number of days to a date; you want to take working days into account: five working days until delivery, or two working days in which the customer needs a response.

Difficult? Not at all. The following nifty AddWorkingDays function does it all for you. Simply pass in a date, along with the number of working days you want to shift the date by. For example, pass in a 5 to get the fifth working day after your date, or "-1" to return the last working day.

Here's the code you'll need:

Public Function AddWorkingDays(ByVal DateIn As DateTime, _
   ByVal ShiftDate As Integer) As DateTime
   ' Adds the [ShiftDate] number of working days to DateIn
   Dim datDate As DateTime = DateIn.AddDays(ShiftDate)
   ' Loop around until we get the need non-weekend day
   While Weekday(datDate) = 1 Or Weekday(datDate) = 7
         datDate = datDate.AddDays(IIf(ShiftDate < 0, -1, 1))
   End While
   Return datDate
End Function

And here's how you might call it in your application:

   Dim datNewDate As DateTime = AddWorkingDays(Today, -1)
   MessageBox.Show("The last working day was " & datNewDate)

About the Author

Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2, $49.99), plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.



About the Author

Karl Moore

Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is the author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2), plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.

Comments

  • What a joke

    Posted by angryresponse on 03/16/2004 05:31pm

    At the very least, I expect a coding samples here to explain and teach the logic; Not just paste a buggy version of half baked logic. This is exactly the code if present in commercial version will cause nightmares. No apparent coding standards. No attempt to explain the logic. Author has not tested the code. Last but not the least, Not all countries in world have holidays on weekday 1 and 7 (Sunday and Saturday).

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Not long ago, security was viewed as one of the biggest obstacles to widespread adoption of cloud-based deployments for enterprise software solutions. However, the combination of advancing technology and an increasing variety of threats that companies must guard against is rapidly turning the tide. Cloud vendors typically offer a much higher level of data center and virtual system security than most organizations can or will build out on their own. Read this white paper to learn the five ways that cloud …

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds