PowerTip: Use PowerShell to identify a leap year


Summary: Use PowerShell and Math to identify a leap year.

Hey, Scripting Guy! Question Is there a way to use PowerShell to identify whether this year is a leap year?

Hey, Scripting Guy! Answer Actually there is! You can use a combination of Get-date to find out the year and the Modulus function in PowerShell.

((Get-Date).year)%4

If the value is zero, the current year is a leap year!

The Doctor

Comments (15)

  1. PetSerAl says:

    2100 year is not leap year. [datetime]::IsLeapYear((Get-Date).Year)

  2. Had to learn all about this stuff for Y2K ... says:

    Rules for leap year: yes if evenly divisible by four, unless also evenly divisible by 100, unless also evenly divisible by 400. So 2000 was a leap year (divisible by 4, 100 and 400), but 2100 will not be (divisible by 4 and 100, but not 400).

    In practice, though, I tend to use .NET for this sort of thing, rather than doing the math myself:

    PS> $Year2000 = Get-Date -Date 2000/01/01
    PS> $Year2100 = Get-Date -Date 2100/01/01
    PS> [System.DateTime]::IsLeapYear($Year2000.Year)
    True
    PS> [System.DateTime]::IsLeapYear($Year2100.Year)
    False
    PS>

  3. Kevinv says:

    Ooooh, never do your own date/time math. The actual rule for leap years is (from wikipedia):

    if (year is not divisible by 4) then (it is a common year)
    else if (year is not divisible by 100) then (it is a leap year)
    else if (year is not divisible by 400) then (it is a common year)
    else (it is a leap year)

    use PetSerAl’s solution instead.

  4. Mario Contreras says:

    This is not accurate. Rules for classifying a year as leap are more than just divisible by 4.

    From http://www.timeanddate.com/date/leapyear.html :

    In the Gregorian calendar three criteria must be taken into account to identify leap years:
    The year can be evenly divided by 4;
    If the year can be evenly divided by 100, it is NOT a leap year, unless;
    The year is also evenly divisible by 400. Then it is a leap year.
    This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.
    Special Leap Year 2000
    The year 2000 was somewhat special as it was the first instance when the third criterion was used in most parts of the world since the transition from the Julian to the Gregorian Calendar.

    If we use .NET, we can determinate if a year is leap or not using the IsLeapYear() static method of the DateTime structure:

    [System.DateTime]::IsLeapYear((Get-Date).Year)

  5. Ed says:

    Indeed A leap year is definitely not year mod 4. Didn’t anyone learn anything from the Y2K fiasco?
    Y2K was also luckily a double exception year for leap year. It would have normally have not been a leap year, as it was mod 100. It was also mod 400, which made is a leap year.

    While somewhat cheating, something along the line of
    (get-date 2/28/2100).AddDays(1).day – 1 is not a leap year, 29 is a leap year.
    tends to be more reliable. It also tends to allow the code be self-correcting, since updates to the OS would tend to fix issues, as opposed to having to update your on code.

  6. BJT says:

    That’s what I wanted to add: if the year can be devided by 100 and not by 400 it’s not a leap year:
    http://support.microsoft.com/en-us/kb/214019

  7. Agreed – every 100 years, the leap year is skipped. Every 400 years it is not.

  8. hpotsirhc says:

    The rule is a little bit more complex: (from https://en.wikipedia.org/wiki/Leap_year)

    if (year is not divisible by 4) then (it is a common year)
    else if (year is not divisible by 100) then (it is a leap year)
    else if (year is not divisible by 400) then (it is a common year)
    else (it is a leap year)

  9. guitarrapc says:

    Please use .NET API [DateTime]::IsLeapYear((Get-Date).Year) as this is proper result. I recommend you to more use .NET as PowerShell’s strength is “it can use Power of .NET”.

  10. Seems there is a flaw in the .NET (isleapyear) method. If you start from this year (2016 which is a leap year) and work your way up the calendar every four years, you’ll see 2100 is in fact a leap year. 84 years away / 4 is twenty one 4 year segments away.

    Basic math works for me 🙂

    Sean
    Honorary Scripting Guy

    1. hpotsirhc says:

      2100 is divisable by 4, of course. But there is another rule, that yearsdivisable by 100 are no leap years (as my Wikipedia-extract stated). 2000 was a leap year, however, because years divisable by 400 are leap years. But 2100 is surely far enough to neglect that. 🙂

      1. Good point there! It’s so far away but when I get my T.A.R.D.I.S. tomorrow I’ll have to go check out the confusion 🙂

        “What do you mean it’s NOT a leap year? REALLY?”

        Thanks for the share. Great trivia in that too 🙂

        Sean
        Honorary Scripting Guy

    2. BrahmsSinger says:

      The .NET method returns the correct answer. It implements the leap year correctly. The definition of a leap year is more complicated than just a modulus of 4. hptosirhc has listed the rule for determining a leap year.

      You could also write a function in PowerShell to do the same as the .NET method

      Function IsLeapYear ($Year) {
      (($Year%4 -eq 0 -and $Year%100 -ne 0) -or $Year%400 -eq 0)
      }

      This returns True or False depending on if the $Year is a leap year or not.

  11. Benjamin Ross says:

    Had to throw in my $0.02

    for ($a = 1900
    $a -le 3000
    $a ++){

    if ((Get-Date -Month 2 -Day 29 -Year $a).month -ne “3”){
    Write-Host (“$a is a leap year”)

    }
    }

  12. Wow! One little Powertip generated a LOT of chatter!

    I love reading the comments and not only getting feedback but sometimes learning from this Great community we have worldwide!

    Thanks everyone!
    Sean
    Honorary Scripting Guy

Skip to main content