将分钟数(整数)及时添加到批处理文件中

时间:2018-11-14 10:37:37

标签: windows batch-file schtasks

我想输入一个整数(这些数字将始终代表分钟),这会增加当前在SCHTASKS命令中使用它的时间。例如,如果当前当前是14:56:03,我在var1中输入9或09,则应在var2中设置15:05:03。我有这个糖果

@echo OFF
echo add minutes
set/p "var1=>"
set/a "var2=%var1%+%time%"
SCHTASKS /change /sd %date% /st %var2% /tn task

我知道这行不通,但我不知道该怎么做。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

我结合了this answer上发布的方法来完成此请求:

@echo OFF
echo add minutes to %time%
set/p "var1=>"

for /F "tokens=1-3 delims=:.," %%a in ("%time%") do set /A "new=(%%a*60+1%%b-100+var1)*60+1%%c-100"
set /A "ss=new%%60+100,new/=60,mm=new%%60+100,hh=new/60,hh-=hh*!(hh-24)"
set "var2=%hh%:%mm:~1%:%ss:~1%"

ECHO SCHTASKS /change /sd %date% /st %var2% /tn task

编辑更改为第二天添加

下一个代码还使用this answer中描述的方法在必要时将日期部分调整为第二天:

@echo OFF
setlocal

echo add minutes
set/p "var1=>"

for /F "tokens=2 delims==" %%a in ('wmic OS Get LocalDateTime /value') do set "dt=%%a"
set /A "D=%dt:~0,8%, MM=1%dt:~4,2%-100, newS=((1%dt:~8,2%-100)*60+1%dt:~10,2%-100+var1)*60+1%dt:~12,2%-100"
set /A "s=newS%%60+100,newS/=60,m=newS%%60+100,h=newS/60,newDD=h/24,h%%=24"
if %newDD% neq 0 (
   set /A "newMM=!( 1%D:~6%-(130+(MM+MM/8)%%2+!(MM-2)*(!(%D:~0,4%%%4)-2)) ), lastMM=!(1%D:~4,2%-112), newYYYY=newMM*lastMM"
   set /A "MM+=newMM*(1-lastMM*12), D+=newYYYY*(20100-1%D:~4%) + !newYYYY*newMM*(100*MM+10000-1%D:~4%) + newDD"
)
set "var2=%h%:%m:~1%:%s:~1%"  &  set "var3=%D:~6,2%/%D:~4,2%/%D:~0,4%"

ECHO SCHTASKS /change /sd %var3% /st %var2% /tn task

答案 1 :(得分:1)

由于输入的分钟偏移量也会更改日期, 我让PowerShell进行计算

@echo off
set /p "var1=enter minutes to add: "
for /f "tokens=1*" %%A in ('
  powershell -NoP -C "(Get-Date).AddMinutes(%var1%).ToString('yyyy/MM/dd HH:mm:ss')"
') do (
  Set "MyDate=%%A"
  set "MyTime=%%B"
)
echo SCHTASKS /change /sd %MyDate% /st %MyTime% /tn task