cmd执行任务时间差计算
@echo off
setlocal enabledelayedexpansion
REM =========================== do something
set start_time=%time%
timeout /t 3
set end_time=%time%
REM =========================== delta time
set /a delta_hours=1%end_time:~0,2%-1%start_time:~0,2%
set /a delta_minutes=1%end_time:~3,2%-1%start_time:~3,2%
set /a delta_seconds=1%end_time:~6,2%-1%start_time:~6,2%
if %delta_seconds% lss 0 (
set /a delta_seconds+=60
set /a delta_minutes-=1
)
if %delta_minutes% lss 0 (
set /a delta_minutes+=60
set /a delta_hours-=1
)
REM =========================== summary
echo Start Time: %start_time%
echo End Time: %end_time%
echo Time elapsed: %delta_hours%:%delta_minutes%:%delta_seconds%
enabledelayedexpansion 延迟计算,cmd命令执行到每一行时会做一个预处理,立即将所有%var%变量进行值替换,如果同一行命令中包含对同一个变量有设值取值操纵可能就无法取得实时值,这个时候就需要通过!var!的方式来尽可能延迟到执行时取值
Delayed Expansion will cause variables within a batch file to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL EnableDelayedExpansion command.