-1

I have an use case where user passes a date from the past. But I need to check if it's a Wednesday. If not, I want to be able to set it to next Wednesday 5 AM. Can somebody please tell me what would be best approach to go about this using PS?

Thanks.

  • Does this answer your question? [PowerShell get weekday name from a date](https://stackoverflow.com/questions/34786585/powershell-get-weekday-name-from-a-date) – Nick Oct 30 '19 at 16:20

1 Answers1

0

Fortunately [datetime] structs have a DayOfWeek property we can use to check that!

Simply advance the date by 1 day at a time until you reach Wednesday:

function Get-UpcomingWednesdayAt5
{
  param(
    [datetime]$Date
  )

  while($Date.DayOfWeek -ne [System.DayOfWeek]::Wednesday){
    # advance 1 day at a time until its wednesday
    $Date = $Date.AddDays(1)
  }

  # Create new [datetime] object with same Date but Time set to 5AM
  return Get-Date -Date $Date -Hour 5 
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206