-1

I want to add some seconds to the current PC time. But I am a beginner in batch script coding and don't know how to modify the current time by adding some seconds.

This is the code I have so far:

Set "tijd=%time%"
echo %tijd%
echo %time%-%tijd%
pause

But the third command line does not output the expected result of current time increased by some seconds.

Mofi
  • 46,139
  • 17
  • 80
  • 143

1 Answers1

0

Here is a commented batch script to get current time without date and add 30 seconds to this time.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Get current time independent on region (country).
for /F "tokens=4-6 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "CurHour=%%I" & set "CurMinute=%%J" & set "CurSecond=%%K" & goto AddSeconds

rem The second value to add must be less than 60 for this code!

rem The code below adds 30 seconds to current time.

rem As second 08 and second 09 would be interpreted as invalid octal
rem numbers and so interpreted with value 0 in an arithmetic expression,
rem there is inserted the character 1 left to the second value to change
rem the second range to 100 to 159 (or 160 on leap second) and adding 30
rem is done by subtracting 70. The same method is used for the minutes 08
rem and 09 and the hours 08 and 09 on incrementing them by one if that is
rem necessary at all.

:AddSeconds
set "NewHour=%CurHour%"
set "NewMinute=%CurMinute%"
set /A NewSecond=1%CurSecond% - 70
if %NewSecond% LSS 60 goto TimeOutput
set /A NewSecond-=60
set /A NewMinute=1%NewMinute% - 99
if %NewMinute% LSS 60 goto TimeOutput
set /A NewMinute-=60
set /A NewHour=1%NewHour% - 99
if %NewHour% LSS 24 goto TimeOutput
set /A NewHour-=24

rem Before the new time is output, the second, minute and hour values are
rem formatted to have always two digits by first inserting 0 at beginning
rem and next use only the last two characters of the second, minute and
rem hour strings assigned to the three environment variables.

:TimeOutput
set "NewSecond=0%NewSecond%"
set "NewSecond=%NewSecond:~-2%"
set "NewMinute=0%NewMinute%"
set "NewMinute=%NewMinute:~-2%"
set "NewHour=0%NewHour%"
set "NewHour=%NewHour:~-2%"

echo Current time: %CurHour%:%CurMinute%:%CurSecond%
echo The new time: %NewHour%:%NewMinute%:%NewSecond%
endlocal

For the command line to get current time read either this answer written by Compo or third part of my answer on same question.

The Windows command processor cmd.exe has no support for time calculations. For that reason it is necessary on using just internal commands of cmd.exe to add 30 seconds to current time with using several arithmetic expressions and IF conditions.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • robocopy /?
  • set /?
  • setlocal /?

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143