-5

I'm looking for an Optimized Function which will return the number of public holidays and weekends between two given dates in C#


Actually the requirement is to combine holidays and weekends to create a good vacation plan for user. I dont need to worry about the country and religion because i have webservices to take care of getting holidays of a specific country and religion

Tim Post
  • 33,371
  • 15
  • 110
  • 174
phanish
  • 45
  • 1
  • 12
  • 4
    What religions, which countries? Too many variable ways to look at this and either way its not going to be a small solution which someone is going to whip up for you without proving you've tried to do this yourself. – Khan Apr 25 '12 at 14:24
  • This seems very difficult seeing as there are so many holidays, and it depends per country etc. – eandersson Apr 25 '12 at 14:24
  • Here's a solution to calculate the number of work hours between two dates: http://stackoverflow.com/a/3835137 If you have a method `bool IsHoliday(DateTime date)` you should be able to easily adapt it to your needs. – dtb Apr 25 '12 at 14:27

1 Answers1

4

Your question can be split into two questions:

  1. How to calculate holidays?
  2. How to calculate the number of holidays between the two given ranges?

The first question has many answers. You could enter them in a data base, download from a website, look for a C# class that calculates the holidays for your culture / environment (there are many!)

Then, you have to iterate through the days from start to end date. This can be done pretty easily:

DateTime startTime = new DateTime(2011, 4, 1);
DateTime endTime = new DateTime(2012, 4, 1);
int numberOfHolidays;

for (DateTime currentDate = startTime; currentDate <= endTime currentDate = currentDate.AddDays(1)) {
    if (isHoliday(currentDate) || isWeekendDay(currentDate))
      numberOfHolidays++;
}

You'll have to implement the methods isHoliday and isWeekendDay by your own, see the answer to the first question part.

Yogu
  • 9,165
  • 5
  • 37
  • 58
  • Actually the requirement is to combine holidays and weekends to create a good vacation plan for user. I dont need to worry about the country and religion because i have webservices to take care of getting holidays of a specific country and religion. – phanish Apr 26 '12 at 04:54
  • Ok, so that's the answer to question 1. Now, try to fetch these information from the website use e.g. my code to calculate the number of holidays. If you get stuck somewhere, ask a new, more specific, question – Yogu Apr 26 '12 at 16:14