HolidayCollectionHolidaysBetween Method

This method returns an enumerable list of holidays between the given years

Definition

Namespace: EWSoftware.PDI
Assembly: EWSoftware.PDI (in EWSoftware.PDI.dll) Version: 2023.1.2.0
public IEnumerable<DateTime> HolidaysBetween(
	int startYear,
	int endYear
)

Parameters

startYear  Int32
The year in which to start generating holiday dates
endYear  Int32
The year in which to stop generating holiday dates

Return Value

IEnumerableDateTime
Returns an enumerable list of DateTime instances representing the holiday dates.

Remarks

This method can be used to get a list of holidays for faster searching when a large number of dates need to be checked. When generating dates, it scans years on either side of the given range too so as to catch holidays that may fall on a weekend and get pushed backwards or forwards into the prior or next year (i.e. New Year's Day may occur on 12/31 if 01/01 is a Saturday).

Example

This example demonstrates the use of the holiday classes and their methods.
C#
DateTime testDate;
int yearFrom = 1998, yearTo = 2006;

// Create a set of fixed and floating holidays
HolidayCollection holidays = new HolidayCollection();

holidays.AddFixed(1, 1, true, "New Year's Day");
holidays.AddFloating(DayOccurrence.Third, DayOfWeek.Monday, 1, 0, "Martin Luther King Day");
holidays.AddFloating(DayOccurrence.Third, DayOfWeek.Monday, 2, 0, "President's Day");
holidays.AddFloating(DayOccurrence.Last, DayOfWeek.Monday, 5, 0, "Memorial Day");
holidays.AddFixed(7, 4, true, "Independence Day");
holidays.AddFloating(DayOccurrence.First, DayOfWeek.Monday, 9, 0, "Labor Day");
holidays.AddFixed(11, 11, true, "Veteran's Day");
holidays.AddFloating(DayOccurrence.Fourth, DayOfWeek.Thursday, 11, 0, "Thanksgiving Day");
holidays.AddFloating(DayOccurrence.Fourth, DayOfWeek.Thursday, 11, 1, "Day After Thanksgiving");
holidays.AddFixed(12, 25, true, "Christmas Day");

// Display the holidays added to the list
Console.WriteLine("Holidays on file.  Is Holiday should be true for all.");

foreach(Holiday hol in holidays)
    Console.WriteLine("Holiday Date: {0:d}   Is Holiday: {1}  Description: {2}",
        hol.ToDateTime(yearFrom), holidays.IsHoliday(hol.ToDateTime(yearFrom)),
        hol.Description);

// Display holidays found in each year specified using the IsHoliday method
Console.WriteLine("Looking for holidays using the IsHoliday method");

testDate = new DateTime(yearFrom, 1, 1);

while(testDate.Year <= yearTo)
{
    if(holidays.IsHoliday(testDate))
        Console.WriteLine("Found holiday: {0:d}", testDate);

    testDate = testDate.AddDays(1);
}

// One more time, but use a hash set using the dates returned by the HolidaysBetween()
// method.  For bulk comparisons, this is faster than the above procedure using the
// IsHoliday method.
Console.WriteLine("Looking for holidays using HolidaysBetween");

var holidayDates = new HashSet<DateTime>(holidays.HolidaysBetween(yearFrom, yearTo));

if(holidayDates.Count != 0)
{
    testDate = new DateTime(yearFrom, 1, 1);

    while(testDate.Year <= yearTo)
    {
        if(holidayDates.Contains(testDate))
            Console.WriteLine("Found holiday: {0:d} {1}", testDate,
                holidays.HolidayDescription(testDate));

        testDate = testDate.AddDays(1);
    }
}

See Also