Learn about Windows PowerShell
Summary: Figure out how many days are in a month by using Windows PowerShell.
How can I use Windows PowerShell to find how many days are in a particular month?
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:
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:
For($i = 1; $i -le 12; $i++)
$sum = $sum + [DateTime]::DaysInMonth(2012,$i)
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.
simple, thank you for sharing.