在带日期的时间间隔之外生成文件
Generate a file outside a time interval with date
我目前正在开发一个程序,该程序会在特定时间范围之外连续 运行 创建文件。这意味着如果当前时间在 starttime
和 endtime
之外,则需要创建文件。
这是我的代码:
$a = get-date -format yyyyMMdd
$b = get-date
while ($true)
{
$starttime = (get-date "21:00") #9pm today
$endtime = (get-date "05:00").AddDays(1) #5am next day
$current = get-date #current time
##Something wrong in my checking method, trying to use 'New-timespan' with no luck
if(($current.TimeOfDay -gt $starttime.TimeOfDay) -and ($current.TimeOfDay -lt $endtime.TimeOfDay)){
$b = get-date
Add-Content "$PSScriptRoot\logging_$($a)_tested.txt" "[$b]-> No Need create file"
} else{
$b = get-date
Add-Content "$PSScriptRoot\logging_$($a)_tested.txt" "[$b]-> Starting create file"
}
start-sleep -Seconds 30
}
问题是我的代码始终处于模式 "Starting create file"。我也尝试使用 New-timespan
,但仍然遇到同样的问题。
我发现了问题,我不得不将 if
语句更改为
if ($current -lt $starttime -or $current -gt $endtime)
我目前正在开发一个程序,该程序会在特定时间范围之外连续 运行 创建文件。这意味着如果当前时间在 starttime
和 endtime
之外,则需要创建文件。
这是我的代码:
$a = get-date -format yyyyMMdd
$b = get-date
while ($true)
{
$starttime = (get-date "21:00") #9pm today
$endtime = (get-date "05:00").AddDays(1) #5am next day
$current = get-date #current time
##Something wrong in my checking method, trying to use 'New-timespan' with no luck
if(($current.TimeOfDay -gt $starttime.TimeOfDay) -and ($current.TimeOfDay -lt $endtime.TimeOfDay)){
$b = get-date
Add-Content "$PSScriptRoot\logging_$($a)_tested.txt" "[$b]-> No Need create file"
} else{
$b = get-date
Add-Content "$PSScriptRoot\logging_$($a)_tested.txt" "[$b]-> Starting create file"
}
start-sleep -Seconds 30
}
问题是我的代码始终处于模式 "Starting create file"。我也尝试使用 New-timespan
,但仍然遇到同样的问题。
我发现了问题,我不得不将 if
语句更改为
if ($current -lt $starttime -or $current -gt $endtime)