Finding Next Sunday

by Mar 22, 2013

If you’d like to find out how long it is until next Sunday (or how many days have passed since last Sunday), simply use the property DayOfWeek. It is a number telling you the current day of the week. So it’s easy to calculate the difference between the current DayOfWeek and the index number of Sunday:

[Int]$index = Get-Date  | Select-Object -ExpandProperty DayOfWeek
$daysTillNextSunday = 7 - $index
$daysSinceLastSunday = $index

$NextSunday = (Get-Date) + (New-TimeSpan -Days $daysTillNextSunday)
$LastSunday = (Get-Date) - (New-TimeSpan -Days $daysSinceLastSunday)