2015-11-12 113 views
0

我有一个批处理文件,将在4小时内循环。我想从现在开始显示4小时(或14400秒)的时间。如何将时间添加到Windows批处理命令%time%输出?

@echo off 
cls 

title My Batch File 

set looptime=14400 

:loopme 

set nextlooptime=%time%+%looptime%  

echo This command prompt will loop in 4 hours, at %nextlooptime%. 

TIMEOUT /T %looptime% /NOBREAK 

goto loopme 

输出与预期不符。

“该命令提示将循环在4小时,在10:51:09.62 + 14400

等待13656秒,按CTRL + C退出...”

我想它显示从%时间%开始的4小时(或14400秒)的时间。

我该怎么做到这一点?

+3

您无法对批次中的时间或日期进行计算。你可以使用'set/a'来进行(有限的)计算。基本上,您需要将时间戳转换为纯整数(秒数),添加循环时间并将生成的整数转换回时间戳。 – Stephan

回答

0

您可以调出Powershell来轻松地将小时添加到当前时间并将其放入一个变量中。

for /f "delims=" %%G IN ('powershell "(get-date %time%).AddHours(4).ToString('HH:mm:ss')"') do set endtime=%%G 
0

这是我的方法。假设其意图仅仅是向用户显示,则秒/毫秒被省略。

@echo off & color f0 

:[ Retrieve the hours and minutes from the %time% variable and turn it into variables ] 
set hour=%time:~0,2% 
    if "%hour:~,1%"=="0" set hour=%hour:~1,2% 
set minute=%time:~3,2% 
:[ Being careful of leading zeros in set /a, as it indicates an octal value ] 
    if "%minute:~,1%"=="0" set minute=%minute:~1,2% 
:[ Here is where 4 hours are added ] 
set /a hour+=4 
:[ Minding the run over time ] 
    if %minute% geq 60 set /a minute=%minute%-60 && set /a hour=%hour%+1 
    if %hour% geq 24 set hour=00 
    if %minute% lss 10 set minute=%minute% 
    if %hour% lss 10 set hour=0%hour:~1,1% 
echo Task will run at %hour%:%minute% 
:[ If desired display is relative to 12; ] 
    if %hour% gtr 12 set /a hour-=12 
echo Alt display is %hour%:%minute% 
pause 

set /a以下:[ Here is where 4 hours are added ]可以调整到意愿。

如果需要灵活性,当然可以使用set /a hour+=%addTime%

相关问题