如何为Windows批处理命令%time%output添加时间?

时间:2015-11-12 16:11:52

标签: windows batch-file time command prompt

我有一个将在4小时内循环的批处理文件。我想显示从现在起4小时(或14400秒)的时间。

@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退出...“

我希望从%time%显示4小时(或14400秒)的时间。

我将如何实现这一目标?

2 个答案:

答案 0 :(得分:0)

你可以调出Powershell,可以很容易地将当前时间添加到一个变量中。

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

答案 1 :(得分: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 ]可以调整为。{/ p>

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