@echo off & setlocal enableextensions
::
:: Get the time and store it in a variable
call :GetDateTimePS start_
::
:: Whatever you have the script to perform
echo Whatever ...
pause
::
:: Get the new time and store it in a variable
call :GetDateTimePS endat_
::
:: Get the difference between the two times
call :TimeDiffPS "%start_%" "%endat_%" elapsed_
::
:: Display the results
echo Start at "%start_%"
echo Ended at "%endat_%"
echo %%elapsed_%%=%elapsed_%
::
endlocal & goto :EOF
:: The indentation is essential to avoid incorrect syntax
:GetDateTimePS
setlocal
for /f "usebackq delims=" %%A in (`powershell -NoLogo -NoProfile -Command ^
"Get-Date -Format 'dd.MM.yyyy HH:mm:ss'"`) do (
set "result_=%%A")
endlocal & set "%1=%result_%" & goto :EOF
:TimeDiffPS
setlocal
for /f "usebackq tokens=1-3 delims=" %%a in (`
powershell -NoProfile -Command ^
"$a=[datetime]::ParseExact('%~1','dd.MM.yyyy HH:mm:ss',$null);"
"$b=[datetime]::ParseExact('%~2','dd.MM.yyyy HH:mm:ss',$null);"
"($b-$a).ToString()"`) do (set result_=%%a)
endlocal & set "%3=%result_%" & goto :EOF
The output might be e.g.
Whatever ...
Press any key to continue . .
Start at "09.08.2026 19:08:00"
Ended at "09.08.2026 19:20:36"
%elapsed_%=00:12:36
A related reverse question of "What was the time 100 minutes ago?" is covered in
item#147, also using
PowerShell-aided command-line.
for a non-PowerShell solution for "Can one calculate the difference
between two times in a script?"