Tag Archives: datetime

Get a Midnight DateTime Value in Powershell

иконографияиконикухненски маси

Some quick methods of obtaining a midnight DateTime value for almost any date.

Clear-Host
# Get Midnight for current date
Get-Date -Hour 0 -Minute 00 -Second 00
# Another way
[datetime]::Today

# Get Midnight for date a week ago
(Get-Date -Hour 0 -Minute 00 -Second 00).AddDays(-7)
# Or use this 
([datetime]::Today).AddDays(-7)

# Get Midnight for date a month ago
Get-Date -Month $((Get-Date -Format "MM") - 1) -Hour 0 -Minute 0 -Second 0

# Get Midnight for a date one year ago
Get-Date -Year $((Get-Date -Format "yyyy") - 1) -Hour 0 -Minute 0 -Second 0