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:

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:

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:

Get-LeapYear

PowerShell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

functionGet-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:

http://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: http://mikefrobbins.com

Twitter: @mikefrobbins

#>

[CmdletBinding()]

param(

[Parameter(ValueFromPipeline=$true)]

[ValidateRange(1582,9999)]

[int[]]$Year=(Get-Date).Year

)

PROCESS{

foreach($yin$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"

}

}

}

}

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 .

My Speaking Engagements

User Groups

Disclaimer

All data and information provided on this site is for informational purposes only. Mike F Robbins (mikefrobbins.com) makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis.