Arithmetic operations with HH:MM:SS times in batch file

后端 未结 4 2004
野的像风
野的像风 2020-12-02 03:02

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          


        
4条回答
  •  悲&欢浪女
    2020-12-02 03:53

    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.

    C:\> set "StartPosition=00:10:30"
    C:\> set "EndPosition=00:10:40"
    C:\> PowerShell.exe -c "$span=([datetime]'%EndPosition%' - [datetime]'%StartPosition%'); '{0:00}:{1:00}:{2:00}' -f $span.Hours, $span.Minutes, $span.Seconds"
    00:00:10
    

    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.

提交回复
热议问题