Proving Leap Year is NOT Every Four Years with PowerShell

Last week, I saw a couple of tweets from PowerShell MVP Jeffery Hicks about leap year that caught my attention:

](/images/2014/03/030614-1.png)

One of the reasons that I found those tweets so interesting is that I had just heard the night before on of all places, Wheel of Fortune, that leap year wasn’t every four years. I’d always been told that leap year was every four years and it has been for my entire life so it was time to investigate further.

I decided to start with one of Jeff’s examples and add some code to it to determine which years should be leap years but weren’t based on the popular belief that leap year is every four years:

$leapYears = 1600..2250 | where {[datetime]::DaysInMonth($_,2) -eq 29}

foreach ($leapYear in $leapYears) {
$nextLeapYear = $leapYear + 4

if ($leapYears -notcontains $nextLeapYear -and $leapYear -ne $leapYears[-1]) {
Write-Output "$nextLeapYear"
}
}

030614-2b.png

Now that’s interesting. Why aren’t those years, shown in the previous example, leap years? I found a Wikipedia article about leap year with the answer to that question which I documented in the description section of the comments in the function shown in the following example. That Wikipedia article also provided some pseudo-code which I converted into PowerShell:

function Get-LeapYear {

<#
.SYNOPSIS
Get-LeapYear is used to determine whether or not a specific year is a leap year.
.DESCRIPTION
Get-LeapYear is a function that is used to determine whether or not the specified
year(s) are leap years. Contrary to popular belief, leap year does not occur every
four years. According to Wikipedia, if a year is divisible by 400 then it's a leap
year, else if the year is divisible by 100 then it's a normal year, else if the year
is divisible by 4 then it's a leap year, else it's a normal year. Source:
https://en.wikipedia.org/wiki/Leap_year
.PARAMETER Year
The year(s) specified in integer form that you would like to determine
whether or not they are a leap year.
.EXAMPLE
Get-LeapYear
.EXAMPLE
Get-LeapYear -Year 2010, 2011, 2012, 2013, 2014, 2015
.EXAMPLE
1890..1910 | Get-LeapYear
.INPUTS
Integer
.OUTPUTS
String
.NOTES
Author:  Mike F Robbins
Website: https://mikefrobbins.com
Twitter: @mikefrobbins
#>

[CmdletBinding()]
param (
[Parameter(ValueFromPipeline=$true)]
[ValidateRange(1582,9999)]
[int[]]$Year = (Get-Date).Year
)

PROCESS {
foreach ($y in $Year) {
if ($y / 400 -is [int]) {
Write-Output "$y is a leap year"
}
elseif ($y / 100 -is [int]) {
Write-Output "$y is not a leap year"
}
elseif ($y / 4 -is [int]) {
Write-Output "$y is a leap year"
}
else {
Write-Output "$y is not a leap year"
}
}
}
}

030614-3a.png

As you can see 1900 was not a leap year and there were 8 years between leap years from 1896 to 1904. Eight years between leap years won’t occur again until years 2096 to 2104 .

ยต