PowerShell Dates & Times
Log rotation, report periods, certificate expiry, "files older than 30 days": dates are everywhere in automation. PowerShell uses the .NET DateTime and TimeSpan types. Get-Date without arguments returns the current moment, so to keep the output of these examples stable they always use fixed dates. In your own scripts, Get-Date is what you'd normally start from.
Creating dates
$d1 = Get-Date -Date '2026-03-14 09:30:00'
$d2 = [datetime]'2026-12-25'
$d3 = Get-Date -Year 2026 -Month 7 -Day 1 -Hour 0 -Minute 0 -Second 0 -Millisecond 0
$d4 = [datetime]::new(2026, 2, 28, 18, 45, 0)
$d1.ToString('yyyy-MM-dd HH:mm')
$d2.DayOfWeek
$d3.ToString('yyyy-MM-ddTHH:mm:ss')
$d4.ToString('dd.MM.yyyy HH:mm')2026-03-14 09:30
Friday
2026-07-01T00:00:00
28.02.2026 18:45
The [datetime]'…' cast always parses in the invariant (US-style) culture, so '03/04/2026' means March 4th. Prefer the unambiguous ISO format yyyy-MM-dd. Get-Date -Date uses the computer's culture.
Parts of a date
$d = Get-Date -Date '2026-03-14 09:30:15'
$d.Year, $d.Month, $d.Day -join '/'
$d.Hour, $d.Minute, $d.Second -join ':'
$d.DayOfWeek
$d.DayOfYear
$d.Date.ToString('yyyy-MM-dd HH:mm:ss') # midnight of the same day
[datetime]::DaysInMonth(2028, 2)
[datetime]::IsLeapYear(2026)2026/3/14
9:30:15
Saturday
73
2026-03-14 00:00:00
29
False
Date arithmetic
Use the Add* methods to move a date. There's no Subtract variant: add a negative number. Subtracting two dates gives a TimeSpan:
$start = [datetime]'2026-01-31'
$start.AddDays(1).ToString('yyyy-MM-dd')
$start.AddMonths(1).ToString('yyyy-MM-dd') # no Feb 31st: clamps to the 28th
$start.AddYears(-1).ToString('yyyy-MM-dd')
$start.AddHours(36).ToString('yyyy-MM-dd HH:mm')
$deadline = [datetime]'2026-06-30 17:00'
$now = [datetime]'2026-06-12 09:15'
$left = $deadline - $now
$left.GetType().Name
"$($left.Days) days, $($left.Hours) hours, $($left.Minutes) minutes"
[math]::Round($left.TotalHours, 1)2026-02-01
2026-02-28
2025-01-31
2026-02-01 12:00
TimeSpan
18 days, 7 hours, 45 minutes
439.8
TimeSpan and New-TimeSpan
A TimeSpan is a duration. Create one with New-TimeSpan or [timespan]:
$shift = New-TimeSpan -Hours 7 -Minutes 30
$break = [timespan]'00:45:00'
$work = $shift - $break
$work.ToString()
$work.TotalMinutes
'{0:hh\:mm}' -f $work
$age = New-TimeSpan -Start '2025-11-02' -End '2026-03-14'
"$($age.Days) days old"
([datetime]'2026-03-14' + (New-TimeSpan -Days 90)).ToString('yyyy-MM-dd')06:45:00
405
06:45
132 days old
2026-06-12
In TimeSpan format strings, the colon must be escaped (hh\:mm), unlike date formats.
Comparing and filtering by date
Dates compare with the normal operators. A typical automation task: find files older than N days. Here we set their timestamps explicitly so the example is repeatable:
New-Item -ItemType Directory ./logs -Force | Out-Null
$files = [ordered]@{ 'jan.log' = '2026-01-10'; 'feb.log' = '2026-02-20'; 'mar.log' = '2026-03-12' }
foreach ($name in $files.Keys) {
$f = New-Item -ItemType File -Path "./logs/$name" -Force
$f.LastWriteTime = [datetime]$files[$name]
}
$today = [datetime]'2026-03-14'
$cutoff = $today.AddDays(-30)
"Cutoff: $($cutoff.ToString('yyyy-MM-dd'))"
Get-ChildItem ./logs | Where-Object LastWriteTime -lt $cutoff |
Sort-Object LastWriteTime | ForEach-Object NameCutoff: 2026-02-12
jan.log
In a real script you'd write $cutoff = (Get-Date).AddDays(-30) and probably end the pipeline with Remove-Item -WhatIf.
Parsing and time zones
$parsed = [datetime]::ParseExact('14/03/2026 21:05', 'dd/MM/yyyy HH:mm', $null)
$parsed.ToString('yyyy-MM-dd HH:mm')
$result = [datetime]::MinValue
$ok = [datetime]::TryParse('not a date', [ref]$result)
"Parsed? $ok"
$utc = [datetime]::SpecifyKind([datetime]'2026-03-14 12:00', 'Utc')
$offset = [datetimeoffset]::new($utc).ToOffset([timespan]'05:30')
$offset.ToString('yyyy-MM-dd HH:mm zzz')
Get-Date -Date $utc -Format 'yyyy-MM-ddTHH:mm:ssZ' -AsUTC2026-03-14 21:05
Parsed? False
2026-03-14 17:30 +05:30
2026-03-14T12:00:00Z
ParseExact is the safe choice for dates in a known format coming from logs or files. For moments in time shared across machines, store UTC (ISO 8601 with Z) or a DateTimeOffset, and convert to local time only for display.
Test yourself
What is ([datetime]'2026-01-31').AddMonths(1)?
- 2026-02-31
- 2026-03-03
- 2026-02-28
- An error
What type do you get from $date2 - $date1?
- DateTime
- Int32
- TimeSpan
- Double
Which format string gives 2026-03-14?
- 'dd-MM-yyyy'
- 'yyyy-mm-dd'
- 'yyyy-MM-dd'
- 'YYYY-MM-DD'