PowerTip: Find How Many Days Are in a Month

PowerTip: Find How Many Days Are in a Month

  • Comments 6
  • Likes

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:


Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • 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)

  • 1..12|%{$sum+=[DateTime]::DaysInMonth(2012,$_)}

  • 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.

  • Nice tip.

  • simple, thank you for sharing.