Here's a generic PowerShell solution (invoked from a batch file—should work on any Windows OS). You didn't say what format the elapsed time should be in, so this is a proof of concept.
SETLOCAL
SET "DATE1=25/06/2018 13:18:25"
SET "DATE2=26/06/2018 09:57:59"
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "((Get-Date -Year %DATE1:~6,4% -Month %DATE1:~3,2% -Day %DATE1:~0,2% -Hour %DATE1:~11,2% -Minute %DATE1:~14,2% -Second %DATE1:~17,2%) - (Get-Date -Year %DATE2:~6,4% -Month %DATE2:~3,2% -Day %DATE2:~0,2% -Hour %DATE2:~11,2% -Minute %DATE2:~14,2% -Second %DATE2:~17,2%)).ToString()" <NUL
This produces
20:39:33.9995000
The date format must have zero-padded values (probably what you described) for this method to work.
If the date is in a format that matches your system locale (I presume you're in Europe or Canada), the PowerShell command can be greatly simplified, allowing Windows to do all the parsing for you. Zero-padding is no longer a requirement in this case.
SETLOCAL
SET "DATE1=25/06/2018 13:18:25"
SET "DATE2=26/06/2018 09:57:59"
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "((Get-Date '%DATE2%') - (Get-Date '%DATE1%')).ToString()" <NUL
If you wanted the elapsed time as a decimal number of seconds, minutes, hours, or days, you could just use, .TotalSeconds
(or .TotalMinutes
, .TotalHours
, or TotalDays
) instead of .ToString()
.
In my opinion, date libraries are the right tool for the job. CMD can only do integer arithmetic and most other logic is painful. If the job can be reduced to simple arithmetic (@aacini's solution is cool in this regard), then it might not be too bad. Otherwise, don't reinvent the wheel.