Finding First and Last Day in Month

by Oct 7, 2016

Did you ever need the first and last day in a given month?

Here is a simple approach:

# specify the date you want to examine
# default is today
$date = Get-Date
$year = $date.Year
$month = $date.Month
    
# create a new DateTime object set to the first day of a given month and year
$startOfMonth = Get-Date -Year $year -Month $month -Day 1 -Hour 0 -Minute 0 -Second 0 -Millisecond 0
# add a month and subtract the smallest possible time unit
$endOfMonth = ($startOfMonth).AddMonths(1).AddTicks(-1)

$startOfMonth
$endOfMonth

Twitter This Tip! ReTweet this Tip!