Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Business/Holiday date handling [closed]

Tags:

c#

I'm looking for a C# class/library that works similarly to the Perl module Date::Manip as far as business/holiday dates. Using that module in Perl, I can pass it a date and find out whether it's a business day (ie, Mon-Fri) or a holiday. Holidays are very simple to define in a config file (see Date::Manip::Holidays). You can enter a 'fixed' date that applies to every year like:

12/25                           = Christmas 

or 'dynamic' dates for every year like:

last Monday in May              = Memorial Day 

or 'fixed' dates for a given year like:

5/22/2010                       = Bob's Wedding 

You can also pass in a date and get back the next/previous business day.

Does anyone know of anything like that in the C# world? I've found a couple of things that implement parts of what I need (http://www.codeproject.com/KB/cs/busdatescalculation.aspx and http://www.codeproject.com/KB/dotnet/HolidayCalculator.aspx) and I can pick them apart and make what I need. But if someone else has already done that, why do it again?

like image 533
DaveKub Avatar asked Sep 14 '10 14:09

DaveKub


People also ask

Is Jan 3rd 2022 a holiday?

Since 1 January 2022, New Year's Day, falls on a Saturday the federal holiday was moved to 31 December 2021. Most public and private sector workers therefore will have a three-day weekend to ring in the new year and return to work on Monday 3 January.

How many public holidays does Singapore have in 2022?

Public Holiday 2022 There will be a total of 6 long weekends in 2022, with those public holidays falling either on a Friday, Sunday, or Monday. The 1 public holiday that falls on a Friday is: Good Friday (15 April)


2 Answers

Nager.Date supports over 110 countries (US, DE, FR, RU, UK, ...) the library is available for .net45 and .netstandard 2.0. The list of supported countries can be found here.

Nuget

PM> install-package Nager.Date 

Example:

Get all publicHolidays of a year

var publicHolidays = DateSystem.GetPublicHoliday(2018, CountryCode.DE); 

Check if a date a public holiday

var date = new DateTime(2018, 01, 01); if (DateSystem.IsPublicHoliday(date, CountryCode.DE)) {     //Yes - New Year's Day } 
like image 126
live2 Avatar answered Oct 08 '22 08:10

live2


A little late to the party, but figure it will help other people googling it. Found a good answer at http://web.archive.org/web/20210128001521/http://geekswithblogs.net/wpeck/archive/2011/12/27/us-holiday-list-in-c.aspx. It even takes into account holidays that fall on a weekend.

private static HashSet<DateTime> GetHolidays(int year)  {      HashSet<DateTime> holidays = new HashSet<DateTime>();      // New Years     DateTime newYearsDate = AdjustForWeekendHoliday(new DateTime(year, 1, 1));      holidays.Add(newYearsDate);       // Memorial Day -- last monday in May      DateTime memorialDay = new DateTime(year, 5, 31);      DayOfWeek dayOfWeek = memorialDay.DayOfWeek;      while (dayOfWeek != DayOfWeek.Monday)      {          memorialDay = memorialDay.AddDays(-1);          dayOfWeek = memorialDay.DayOfWeek;      }      holidays.Add(memorialDay);           // Independence Day     DateTime independenceDay = AdjustForWeekendHoliday(new DateTime(year, 7, 4));      holidays.Add(independenceDay);        // Labor Day -- 1st Monday in September      DateTime laborDay = new DateTime(year, 9, 1);      dayOfWeek = laborDay.DayOfWeek;      while(dayOfWeek != DayOfWeek.Monday)      {          laborDay = laborDay.AddDays(1);          dayOfWeek = laborDay.DayOfWeek;      }      holidays.Add(laborDay);      // Thanksgiving Day -- 4th Thursday in November      var thanksgiving = (from day in Enumerable.Range(1, 30)                     where new DateTime(year, 11, day).DayOfWeek == DayOfWeek.Thursday                     select day).ElementAt(3);      DateTime thanksgivingDay = new DateTime(year, 11, thanksgiving);      holidays.Add(thanksgivingDay);      // Christmas Day      DateTime christmasDay = AdjustForWeekendHoliday(new DateTime(year, 12, 25));      holidays.Add(christmasDay);       // Next year's new years check     DateTime nextYearNewYearsDate = AdjustForWeekendHoliday(new DateTime(year + 1, 1, 1));     if (nextYearNewYearsDate.Year == year)     holidays.Add(nextYearNewYearsDate);      return holidays;  }  public static DateTime AdjustForWeekendHoliday(DateTime holiday)  {      if (holiday.DayOfWeek == DayOfWeek.Saturday)      {          return holiday.AddDays(-1);      }      else if (holiday.DayOfWeek == DayOfWeek.Sunday)      {          return holiday.AddDays(1);      }      else      {          return holiday;      }  } 
like image 34
Todes3ngel Avatar answered Oct 08 '22 10:10

Todes3ngel