There are a lot of examples on how to achieve these conversions in a Batch file. However, most of those methods are rudimentary and crude.
I developed a very efficient method to get the elapsed seconds between two times, but I never had completed the equivalent efficient method to get the elapsed days between two dates. So here it is:
@echo off
setlocal EnableDelayedExpansion
rem Get elapsed days/time between two timestamps in "DD/MM/YYYY HH:MM:SS" format
rem Antonio Perez Ayala aka Aacini
rem Define the "Date in DDMMYYYY format" To "Julian Day Number" conversion "function"
set "DateToJDN(Date)=( a=1Date, y=a%%10000, a/=10000, m=a%%100, d=a/100-100, a=(m-14)/12, (1461*(y+4800+a))/4+(367*(m-2-12*a))/12-(3*((y+4900+a)/100))/4+d-32075 )"
echo Enter two timestamps in "DD/MM/YYYY HH:MM:SS" format
set /P "stamp1=Enter start timestamp: "
set /P "stamp2=Enter end timestamp: "
for /F "tokens=1-4" %%a in ("%stamp1% %stamp2%") do set "date1=%%a" & set "time1=%%b" & set "date2=%%c" & set "time2=%%d"
set /A "days=!DateToJDN(Date):Date=%date2:/=%! - !DateToJDN(Date):Date=%date1:/=%!"
set /A "ss=(((1%time2::=-100)*60+1%-100) - (((1%time1::=-100)*60+1%-100)"
if %ss% lss 0 set /A "ss+=60*60*24, days-=1"
set /A "hh=ss/3600+100, ss%%=3600, mm=ss/60+100, ss=ss%%60+100"
echo/
echo Duration: %days% days and %hh:~1%:%mm:~1%:%ss:~1%
You may review further information about this program at these links: the efficient conversion method for the time part is described at this answer, the JulianDayNumber to Date conversions method is described at this reply, and the way to define "functions" in Batch is described at this thread.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…