June 21st, 2016

PowerTip: Use PowerShell to identify a leap year

Doctor Scripto
Scripter

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

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

1 comment

Discussion is closed. Login to edit/delete existing comments.

Newest
Newest
Popular
Oldest
  • Joakim Hellström

    First of all. There is no leap year if the year can be divided with 100, except if it’s possible to divide by 400. So 1600, 2000 and 2400 are leap year but not 1700, 1800, 1900.Secondly. You can use [datetime]::IsLeapYear

Feedback