Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

Calculate a duration between two dates ( DD/MM/YYYY HH:MM:SS) in batch file

I would like to calculate a duration between two dates which have DD/MM/YYYY HH:MM:SS as format:

echo Jours;Mois;Année;hh;mm;JoursF;MoisF;AnnéeF;hhF;mmF;Duration>Duration.csv

REM ====Découper la date en jours, mois et années ==
SET AA1=2018
SET MM1=01
SET JJ1=01

REM REM ====Découper l'heures en heures, Minutes, Secondes ==

SET hh1=01
SET mmm1=20

REM =====saisie deux 

REM ====Découper la date en jours, mois et années ==
SET AA2=2019
SET MM2=02
SET JJ2=03

REM ====Découper l'heures en heures, Minutes, Secondes ==

SET hh2=02
SET mmm2=5

REM==== statement condition
IF %AA2% EQU %AA1% IF %MM2% EQU %MM1% IF %JJ2% EQU %JJ1% IF %hh2% EQU %hh1% 
IF %mmm2% GTR %mmm1% set /a duration = %mmm2%-%mmm1%

REM===== display duration 

Echo %JJ1%;%MM1%;%AA1%;%hh1%;%mmm1%;%JJ2%;%MM2%;%AA2%;%hh2%;%mmm2%;%duration% >>Duration.csv
question from:https://stackoverflow.com/questions/65894895/difference-between-two-dates-number-of-days-between-two-dates

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...