Return weekdays and none holiday

How can I include holidays in my code. Right now it just return weekdays. I want it to return weekday and also none holidays.

Public Function AddBusinessDays(current As DateTime, days As Integer) As DateTime
    Dim sign = Math.Sign(days)
    Dim unsignedDays = Math.Abs(days)
    For i As Object = 0 To unsignedDays - 1
        Do
            current = current.AddDays(sign)
        Loop While current.DayOfWeek = DayOfWeek.Saturday OrElse current.DayOfWeek = DayOfWeek.Sunday
    Next
    Return current
End Function
Public Function AddBusinessDays_list(current As DateTime, days As Integer) As List(Of String)

    Dim result As New List(Of String)
    Dim sign = Math.Sign(days)
    Dim unsignedDays = Math.Abs(days)
    For i As Object = 0 To unsignedDays - 1
        Do
            current = current.AddDays(sign)
            If current.DayOfWeek <> DayOfWeek.Saturday And current.DayOfWeek <> DayOfWeek.Sunday Then  
            result.Add(current.ToString("yyyy-MM-dd")
            End If

        Loop While current.DayOfWeek = DayOfWeek.Saturday OrElse current.DayOfWeek = DayOfWeek.Sunday
    Next
    Return result
End Function

Public Function SubtractBusinessDays(current As DateTime, days As Integer) As List(Of String)
    Return AddBusinessDays_list(current, -days)
End Function

Open in new window

VBdotnet2005Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
Holidays are different in each country, and even between different regions of the same country. And there could be changes from year to year. So there is no way for Microsoft to put that into the framework.

Your best bet is to built a list of holiday dates, in something like a List (Of Date), and add a lookup to that list in your verification.
it_saigeDeveloperCommented:
I will note that while Jame's recommendation is perfectly valid, Kaufmed (one of the experts here at EE) has a wonderful library that includes a holiday class.

You can find his presentation of the holiday class here: http:/Q_28694411.html

-saige-
käµfm³d 👽Commented:
If you use the code I posted in that question, make sure you test that code in your environment. I seem to recall there was another issue with the calculations, but I cannot recall what it is at the moment. I can try to look at it tonight when I'm home.

Also, I don't think I did anything too crazy in there, so you should be able to post those code files into any of the online converters, since it appears that you are in VB.NET and the code is C#. That, or you can just compile the project and reference the generated DLL.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

VBdotnet2005Author Commented:
I can use these for previous working day and check if a date is a holiday.
            
            Dim predays As Date = GetPreviousWorkingDay(Now)
            Dim predays_holiday As Boolean = IsHoliday(Now)
            
            My question is what do I need to pass to get a previous working with holidays into account? Can you give me a sample?
            
        
            Dim predays As Date = GetPreviousWorkingDay(Now, holidayCalculator:= ???)
            Public Shared Function GetPreviousWorkingDay([date] As DateTime, Optional options As HolidayOptions = Nothing) As DateTime
                  Return GetPreviousWorkingDay([date], Holiday.IsHoliday, options)
            End Function
käµfm³d ,
            Public Shared Function GetPreviousWorkingDay([date] As DateTime, holidayCalculator As Func(Of DateTime, HolidayOptions, Boolean), Optional options As HolidayOptions = Nothing) As DateTime
                  Dim workingDay As DateTime = GetWorkingDayUsingOffset([date], -1, holidayCalculator, options)

                  Return workingDay
            End Function
käµfm³d 👽Commented:
In the basic case, it should simply be:

Dim predays As Date = GetPreviousWorkingDay(Now)

Open in new window


I've used default parameters so that you don't have to specify HolidayOptions if you don't need to tweak them from the default. The idea behind the "holidayCalculator" overload was that you could pass in your own delegate to determine what a holiday is. This was centered around people in other countries who might use that code--I based the code on U.S. holidays since that's where I live. If one be so inclined, he could just modify the Holiday class to include the relevant holidays. But he could also just create and pass the delegate. I wanted to be flexible.

I'm reviewing the code and the tests now to see if I can remember what the issue I saw before was. I want to say there's a stack overflow somewhere. but only under certain conditions. I'll post back when I find it.
käµfm³d 👽Commented:
It seems I did some revamping after the original writing. I've compared the original code to the most recent version, and there are a few changes. I guess I fixed whatever issue I observed before was. I've added a bunch more unit tests, and they're all passing, so that assures me a bit more. You can review the unit tests for GetPreviousWorkingDay to see how it's called--it's just as I described before.

The new version of the project can be acquired here:  https://filedb.experts-exchange.com/incoming/ee-stuff/8432-WorkingDayCalculator.zip

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
While I am appreciative of the gesture, I'd suggest slipping it_saige some (if not most) of those points. He pointed out the existence of the library; I only popped in because I knew it still had some issues  = )
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.