user2637202
user2637202

Reputation: 133

Question about diff date-time in powershell

I need to run a code only during business hours. First I check if the hour is lower than 8AM or higher than 7PM, if it is not met I calculate the time difference the next day and put a Sleep.

#Run the script on business hours
$hour = [int](Get-Date -Format HH)
If ($hour -le 8 -or $hour -gt 18) { 

    $date = Get-Date
    $date = $date.AddDays(1)
    $mmddyyy = $date.ToString("dd/MM/yyyy")
    $nextDy = New-TimeSpan -End "$mmddyyy 08:00"
    Start-Sleep -Seconds $nextDy.TotalSeconds
}

#<code to execute. I don't use functions. A very simple script>

The problem I see is that if it is for example 1AM, the script will not run until the next day instead of waiting only 7 hours.

Upvotes: 2

Views: 195

Answers (3)

js2010
js2010

Reputation: 27606

Here's my take and date math. Before 8am is a different case -- I don't add a day to the sleep time. I assume you mean 7pm not '$hour -gt 18' or 6pm. '8am' and '7pm' automatically get converted to [datetime] in a comparison.

$hour = get-date
if ($hour -le '8am') {
  $nextDy = [datetime]'8am' - $hour
  start-sleep $nextDy.totalseconds
} elseif ($hour -gt '7pm') {
  $nextDy = [datetime]'8am' + '1' <#day#> - $hour
  start-sleep $nextDy.totalseconds
}

Upvotes: 0

Daniel
Daniel

Reputation: 5112

#Run the script on business hours
$hour = [int](Get-Date -Format HH)
If ($hour -le 8 ) {
    # Get today at 8am
    $businessStart = [datetime]'08:00'

    # Get difference timespan between business start and now
    $difference = $businessStart - (Get-Date)

    # Get number of seconds to wait (rounded up)
    $totalSecondsToWait = [System.Math]::Ceiling($difference.TotalSeconds)

    Start-Sleep -Seconds $totalSecondsToWait
}
elseif ($hour -gt 18) {
    # Get tomorrow at 8am
    $businessStart = ([datetime]'08:00').AddDays(1)

    # Get difference timespan between business start and now
    $difference = $businessStart - (Get-Date)
    
    # Get number of seconds to wait (rounded up)
    $totalSecondsToWait = [System.Math]::Ceiling($difference.TotalSeconds)
    
    Start-Sleep -Seconds $totalSecondsToWait
}

Upvotes: 2

Lee_Dailey
Lee_Dailey

Reputation: 7489

here's another way to get "in or out of office hours". it uses the .Hour property of a [datetime] object. [grin]

what it does ...

  • sets the constants
  • generates a datetime object with the desired hour for testing the comparison
  • derives the current hour
  • tests to see if that hour is in the office hours range
  • outputs the result to the screen

the code ...

$StartWorkHour = 8
$EndWorkHour = 19
$OfficeHourRange = $StartWorkHour..$EndWorkHour

$Now = Get-Date -Hour 20
#$Now = Get-Date -Hour 9
$CurrentHour = $Now.Hour
if ($CurrentHour -notin $OfficeHourRange)
    {
    Write-Host ('The current time [ {0} ] is NOT in office hours.' -f $Now.ToString('HH:mm'))
    }
    else
    {
    Write-Warning ('    [ {0} ] is during office hours.' -f $Now.ToString('HH:mm'))
    }

output with each of the two hour settings ...

The current time [ 20:56 ] is NOT in office hours.
WARNING:     [ 09:56 ] is during office hours.

Upvotes: 4

Related Questions