PowerTip: Find How Many Days Are in a Month

Summary: Figure out how many days are in a month by using Windows PowerShell.

Hey, Scripting Guy! Question How can I use Windows PowerShell to find how many days are in a particular month?

Hey, Scripting Guy! Answer Use the DaysInMonth static method from the System.DateTime object.
            It expects the year and then the month as arguments. Here are a couple examples:

Days in October 2014:


Days in February 2015:


Comments (5)

  1. simple, thank you for sharing.

  2. Steve Erck says:

    As an exercise I wanted to sum the days in a year, to see if I could figure the syntax. This is what I got – would love to hear any suggestions on how to improve or streamline:

    clear-variable sum
    For($i = 1; $i -le 12; $i++)
    $sum = $sum + [DateTime]::DaysInMonth(2012,$i)

  3. The Mad Adder says:


  4. Steve Erck says:

    Nice, I knew there was a cleaner way! I had tried getting the range to work but gave up and went with the for loop instead. Thanks for posting.