Pages

Friday, September 12, 2014

State Holidays & Importing Into Outlook.


The other day, I started planning out vacations for the next year and realized I didn't have updated holidays in my calendar. My office provides us the same days off as the State of California, so I used their rules when creating this script.
  • January 1* (New Year’s Day)
  • Third Monday in January (Martin Luther King Jr. Day)
  • Third Monday in February (President’s Birthday)
  • March 31 (Cesar Chavez Day)
  • Last Monday in May (Memorial Day)
  • July 4* (Independence Day)
  • First Monday in September (Labor Day)
  • November 11** (Veteran's Day)
  • Thanksgiving Day and Day After Thanksgiving
  • December 25* (Christmas Day)

 The following Monday shall be observed as the holiday for any holiday which falls on a Sunday.
 *Holiday credit is accrued if any of these dates occur on a Saturday.
**For Veteran’s Day, the preceding Friday shall be observed as the holiday on years when November 11 falls on a Saturday.

The intention of this script is to create a Holiday file, that can be imported into Outlook. The basic format of this file is:

[Category] (# of events in category)
Title,Date
Title,Date

or 

[Expense Reports] 4
Q1 Expense Reports Due, 2007/04/15
Q2 Expense Reports Due, 2007/11/15
Q3 Expense Reports Due, 2007/12/15
Q4 Expense Reports Due, 2007/01/15


Running this script will generate output the 11 State holidays for the year that you specify when running the script.



  
.\Report-StateHolidays.ps1 2020

I then feed that into a simple write-host that formats the data into the HOL file format.

  
.\Report-StateHolidays.ps1 2020 | %{write-host $_.holiday,$_.date -Separator ","}

New Years Day,2020/01/01
Martin Luther King Jr.,2020/01/20
President's Day,2020/02/17
Cesar Chavez Day,2020/03/31
Memorial Day,2020/05/25
Independence Day,2020/07/04
Labor Day,2020/09/07
Veteran's Day,2020/11/11
Thanksgiving,2020/11/26
Day After Thanksgiving,2020/11/27
Christmas Day,2020/12/25

Open Notepad, create header, then copy and paste the script info. Repeat for all years you want the data.

[State Holidays 2020] 11
New Years Day,2020/01/01
Martin Luther King Jr.,2020/01/20
President's Day,2020/02/17
Cesar Chavez Day,2020/03/31
Memorial Day,2020/05/25
Independence Day,2020/07/04
Labor Day,2020/09/07
Veteran's Day,2020/11/11
Thanksgiving,2020/11/26
Day After Thanksgiving,2020/11/27
Christmas Day,2020/12/25


Then save the file, replacing .TXT with .HOL, like StateHolidays2020.HOL
Double-click the file and click the categories you want to import.




  
<# .SYNOPSIS
Report-StateHolidays.PS1: Find State Holiday's for specific year .DESCRIPTION Will generate dates suitable for creating Holiday file to import into Outlook. .PARAMETER <YEAR> Year want to find holiday's on. .EXAMPLE .\Report-StateHolidays.ps1 2020 | %{write-host $_.holiday,$_.date -Separator ","} January 1* (New Year’s Day) Third Monday in January (Martin Luther King Jr. Day) Third Monday in February (President’s Birthday) March 31 (Cesar Chavez Day) Last Monday in May (Memorial Day) July 4* (Independence Day) First Monday in September (Labor Day) November 11** (Veteran's Day) Thanksgiving Day and Day After Thanksgiving December 25* (Christmas Day) The following Monday shall be observed as the holiday for any holiday which falls on a Sunday. *Holiday credit is accrued if any of these dates occur on a Saturday. **For Veteran’s Day, the preceding Friday shall be observed as the holiday on years when November 11 falls on a Saturday. #> [CmdLetBinding()] param( [parameter(Position=0,Mandatory=$true,ValueFromPipelineByPropertyName=$true)] [string]$year ) $Holidays=@() #$FindMonday will find offset to first monday in Month. $FindMonday["Sunday"] will return 1. $FindMonday = @{"Sunday"=1;"Monday"=0;"Tuesday"=6;"Wednesday"=5;"Thursday"=4;"Friday"=3;"Saturday"=2} #Same as FindMonday, but biased for Thursday. Only used for US-Thanksgiving date. $FindThursday = @{"Sunday"=4;"Monday"=3;"Tuesday"=2;"Wednesday"=1;"Thursday"=0;"Friday"=6;"Saturday"=5} $Holiday = "New Years Day" $date = "1/1/"+$year $newYear = Get-Date $($date) if ($newyear.DayOfWeek -eq "Sunday") {$newYear=$newyear.AddDays(1)} $UserObject = New-Object psobject $UserObject | Add-Member -MemberType noteproperty -Name "Holiday" -Value $Holiday $UserObject | Add-Member -MemberType noteproperty -Name "Date" -Value $newyear.ToString("yyyy/MM/dd") $Holidays += $UserObject $Holiday = "Martin Luther King Jr." #Find 3rd Monday in Month. First Monday + 2 weeks. $mlk = $newYear.AddDays(14+$FindMonday[$newYear.DayOfWeek.tostring()]) $UserObject = New-Object psobject $UserObject | Add-Member -MemberType noteproperty -Name "Holiday" -Value $Holiday $UserObject | Add-Member -MemberType noteproperty -Name "Date" -Value $MLK.ToString("yyyy/MM/dd") $Holidays += $UserObject $Holiday = "President's Day" $February = Get-Date $("2/1/"+$year) #Find 3rd Monday in Month. First Monday + 2 weeks. $Presidents = $February.AddDays(14+$FindMonday[$February.DayOfWeek.tostring()]) $UserObject = New-Object psobject $UserObject | Add-Member -MemberType noteproperty -Name "Holiday" -Value $Holiday $UserObject | Add-Member -MemberType noteproperty -Name "Date" -Value $Presidents.ToString("yyyy/MM/dd") $Holidays += $UserObject $Holiday = "Cesar Chavez Day" $date = "3/31/"+$year $CC = Get-Date $($date) if ($CC.DayOfWeek -eq "Sunday") {$CC = $CC.AddDays(1)} $UserObject = New-Object psobject $UserObject | Add-Member -MemberType noteproperty -Name "Holiday" -Value $Holiday $UserObject | Add-Member -MemberType noteproperty -Name "Date" -Value $CC.ToString("yyyy/MM/dd") $Holidays += $UserObject $Holiday = "Memorial Day" $May = Get-Date $("5/1/"+$year) #Last Monday of Month = 4th Monday of month. This may break down if $memorial = $May.AddDays(21+$FindMonday[$May.DayOfWeek.tostring()]) $UserObject = New-Object psobject $UserObject | Add-Member -MemberType noteproperty -Name "Holiday" -Value $Holiday $UserObject | Add-Member -MemberType noteproperty -Name "Date" -Value $memorial.ToString("yyyy/MM/dd") $Holidays += $UserObject $Holiday = "Independence Day" $date = "7/4/"+$year $ID = Get-Date $($date) #if ($ID.DayOfWeek -eq "Saturday") {Write-Host "HC"} if ($ID.DayOfWeek -eq "Sunday") {$ID = $ID.AddDays(1)} #Write-Host $Holiday,"`t",$ID.tolongdateString() $UserObject = New-Object psobject $UserObject | Add-Member -MemberType noteproperty -Name "Holiday" -Value $Holiday $UserObject | Add-Member -MemberType noteproperty -Name "Date" -Value $ID.ToString("yyyy/MM/dd") $Holidays += $UserObject $Holiday = "Labor Day" $Sept = Get-Date $("9/1/"+$year) $ld = $Sept.AddDays($FindMonday[$Sept.DayOfWeek.tostring()]) $UserObject = New-Object psobject $UserObject | Add-Member -MemberType noteproperty -Name "Holiday" -Value $Holiday $UserObject | Add-Member -MemberType noteproperty -Name "Date" -Value $LD.ToString("yyyy/MM/dd") $Holidays += $UserObject $Holiday = "Vetern's Day" # Always celebrate holiday, Saturday falls to Friday, Sunday goes to Monday. $date = "11/11/"+$year $vets = Get-Date $($date) if ($vets.DayOfWeek -eq "Saturday") {$vets = $vets.AddDays(-1)} if ($vets.DayOfWeek -eq "Sunday") {$vets = $vets.AddDays(1)} $UserObject = New-Object psobject $UserObject | Add-Member -MemberType noteproperty -Name "Holiday" -Value $Holiday $UserObject | Add-Member -MemberType noteproperty -Name "Date" -Value $VETS.ToString("yyyy/MM/dd") $Holidays += $UserObject $Holiday = "Thanksgiving" $nov = Get-Date $("11/1/"+$year) #Find 4th Thursday in November. $Thanks = $nov.AddDays(21+$FindThursday[$nov.DayOfWeek.tostring()]) $UserObject = New-Object psobject $UserObject | Add-Member -MemberType noteproperty -Name "Holiday" -Value $Holiday $UserObject | Add-Member -MemberType noteproperty -Name "Date" -Value $Thanks.ToString("yyyy/MM/dd") $Holidays += $UserObject $Holiday = "Day After Thanksgiving" #No working on Black Friday. $BF = $Thanks.AddDays(1) $UserObject = New-Object psobject $UserObject | Add-Member -MemberType noteproperty -Name "Holiday" -Value $Holiday $UserObject | Add-Member -MemberType noteproperty -Name "Date" -Value $BF.ToString("yyyy/MM/dd") $Holidays += $UserObject $Holiday = "Christmas Day" $date = "12/25/"+$year $xmas = Get-Date $($date) if ($xmas.DayOfWeek -eq "Sunday") {$xmas = $xmas.AddDays(1)} $UserObject = New-Object psobject $UserObject | Add-Member -MemberType noteproperty -Name "Holiday" -Value $Holiday $UserObject | Add-Member -MemberType noteproperty -Name "Date" -Value $XMAS.ToString("yyyy/MM/dd") $Holidays += $UserObject Return $Holidays

No comments:

Post a Comment