In one of my batch scripts I need to calculate the duration of an interval in a video file. First the user is asked to input the start and end times:
set /p StartPosition=Start position (HH:MM:SS):
set /p EndPosition=End position (HH:MM:SS):
Then, I would like the batch script to calculate the duration in between.
How can I subtract %StartPosition%
from %EndPosition%
like this, for example:
00:10:40 - 00:10:30 = 00:00:10
The reason why I can't figure out how to do this is because these numbers are separated by colons.
Edit: This question is different to this question because I do not need the scrip to treat the numbers as time values.
EDIT: Some explanations added
This program use the usual method to convert a time in HH:MM:SS format into a number of seconds via the standard formula:
seconds = (HH*60+MM)*60+SS
. However, theset /A
command consider the numbers that start with0
as written in octal base, and hence08
and09
would be invalid octal numbers. To avoid this problem, a digit1
is placed before expand the number and a100
is subtracted after, so ifHH=08
then1%HH%-100
correctly gives8
; that is:There are several methods to split a time given in HH:MM:SS format into its three parts. For example, if we take
set EndPosition=HH:MM:SS
as base, then we may use afor /F
command this way:In this program a different method is used. If we match the original
EndPosition=HH:MM:SS
string with the desired formula, we may construct this mapping scheme:In other words: if we replace the colons of the original string by
-100)*60+1
and insert((1
at beginning and-100
at end, we obtain the desired formula; that is:This is a very efficient method that even allows to replace both EndPosition and StartPosition strings in the same formula (enclosing both parts in parentheses) and directly subtract them:
You may cancel the
@echo off
command and run the program to review the exact formula that is evaluated after the values of the variables are replaced. For example, whenStartPosition=00:10:30
andEndPosition=00:10:40
, this is the expression that is evaluated:Just to complete this description, this is the "standard" way to evaluate the same formula using a
for /F
command:The opposite conversion from number of seconds to HH:MM:SS parts is straightforward:
However, each part in the result must be displayed with two digits, but this formatting may be achieved in a very simple way. Instead of insert three
if
commands that check if each part is less than 10 and insert a padding zero in such a case, the number100
is just added to the parts (converting an8
into108
, for example), and when each part is displayed the first digit is omitted (so just08
is shown). This is a very efficient method to format numbers that may be performed in the sameset /A
command used to obtain the parts. For example:In this way, the conversion of two times into two number of seconds, their subtraction and the opposite conversion and formatting to HH:MM:SS is performed in two SET /A commands, that even may be written in a single, long line.
Output examples:
Here's a working prototype:
Output:
Its not complete, but its a reasonable start.
This is possible to do in pure batch by parsing each field as an independent string, then doing arithmetic on them. Many practical solutions call into some other program to do the date math.
The following code calls into PowerShell to use the .NET
DateTime
class to do the parsing for you.This executes two lines of PowerShell code; one to convert both times into
DateTime
objects and subtract them, and the other to output the result in the format you specified.I think, @Aacini has cleared Everything here. He got you, Before I Do. But, I want to Improved on him as - by using For Loop to make code Easier.
Note: Everything after 'REM' is a Comment for the sake of understanding easily...
All You need to DO is Copy It into Your Batch File. And, Use it as follows (in your main code):
Syntax: Call :Time [Your Time 1] [Operation] [Your Time 2]
And, You can Now apply - any operation - including 'Addition, Substraction, Division, Multiplication' ;) The Time Function
--------------Copy the Below Code----------
You can Also visit, my Website - based on Batch Programming. (www.thebateam.org) You'll find alot of stuff there - to help you out. :) Here's the Final Output - When I saved the Code in Answer.bat File