A pedant that hangs out in the dark corner-cases of the web.

Saturday, December 11, 2010

US Federal Holidays in PowerShell

Here's how to determine whether a date is a US federal holiday using PowerShell.

function Test-USFederalHoliday
{
<#
.Synopsis
Returns the name of the holiday of a date, if it is a U.S. federal holiday.
.Description
The following holidays are checked:
* New Year's Day, January 1 (± 1 day, if observed)
* Martin Luther King, Jr. Day, Third Monday in January
* President's Day, Third Monday in February
* Memorial Day, Last Monday in May
* Independence Day, July 4 (± 1 day, if observed)
* Labor Day, First Monday in September
* Columbus Day, Second Monday in October
* Veteran's Day, November 11 (±1 day, if observed)
* Thanksgiving Day, Fourth Thursday in November
* Christmas Day, December 25 (±1 day, if observed)
#>
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)][datetime]$Date,
[Parameter(Mandatory=$false,HelpMessage='Are Saturday holidays observed on Friday?')][switch]$SatToFri,
[Parameter(Mandatory=$false,HelpMessage='Are Sunday holidays observed on Monday?')][switch]$SunToMon
)
$MMdd= '{0:MMdd}' -f $Date
switch($Date.DayOfWeek)
{
'Monday'
{
switch -regex ($MMdd)
{
'^01(?:1[5-9]|2[01])$' {return 'Martin Luther King, Jr. Day'}
'^02(?:1[5-9]|2[01])$' {return 'Presidents Day'}
'^05(?:2[5-9]|3[01])$' {return 'Memorial Day'}
'^090[1-7]$' {return 'Labor Day'}
'^10(?:0[89]|1[01-4])$' {return 'Columbus Day'}
}
if($SunToMon)
{
switch($MMdd)
{
'0102' {return 'New Year''s Day (Observed)'}
'0705' {return 'Independence Day (Observed)'}
'1112' {return 'Veteran''s Day (Observed)'}
'1226' {return 'Christmas Day (Observed)'}
}
}
}
'Thursday'
{
if($MMdd -match '^112[2-8]$') {return 'Thanksgiving Day'}
}
'Friday'
{
if($SatToFri)
{
switch($MMdd)
{
'1231' {return 'New Year''s Day (Observed)'}
'0703' {return 'Independence Day (Observed)'}
'1110' {return 'Veteran''s Day (Observed)'}
'1224' {return 'Christmas Day (Observed)'}
}
}
}
}
switch($MMdd)
{
'0101' {return 'New Year''s Day'}
'0704' {return 'Independence Day'}
'1111' {return 'Veteran''s Day'}
'1225' {return 'Christmas Day'}
}
}

2 comments:

Anonymous said...

Hello to all, how is everything, I think every one is getting more from
this web page, and your views are good designed for new viewers.



Here is my web site; continue reading

Anonymous said...

Hello to all, how is everything, I think every one is getting more from this web page, and your views are good
designed for new viewers.

my blog ... continue reading
Also see my website :: click here to go there now