Date & Time Calculations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > Date and Time Calculation > DTO. - Date Time Operations >

Date & Time Calculations

DTO.TimeDiff - Calculate Seconds Between Times

Previous Top Next


MiniRobotLanguage (MRL)

 

DTO.TimeDiff Command

Calculate the Difference in Seconds Between Two Times

 

Intention

 

The DTO.TimeDiff command calculates the duration between two specified times and returns the result in total seconds. This is extremely useful for measuring elapsed time, calculating process durations, or comparing timestamps.

 

The command performs the calculation time1 - time2. The result is a signed integer: it is positive if `time1` is later than `time2`, negative if `time1` is earlier, and zero if they are identical. The command assumes both times occur on the same day.

 

Syntax

 

DTO.TimeDiff|time1|time2|[format]|[$$RES]

 

Parameter Explanation

 

time1 - The first time string (the minuend). Can be a variable or literal.

 

time2 - The second time string (the subtrahend). Can be a variable or literal.

 

format - (Optional) The format mask for both `time1` and `time2`. If omitted, the default "HH:MM:SS" is used.

 

$$RES - (Optional) The variable to store the integer result (total seconds of difference).

 

Example

 

'**********************************************

' DTO.TimeDiff - Sample

'**********************************************

'

' --- Sample 1: Calculate positive difference (duration) ---

$$TAA = "14:30:00" ' End time

$$TAB = "14:00:00" ' Start time

DTO.TimeDiff|$$TAA|$$TAB||$$DIF

MBX.Duration is $$DIF seconds. ' Result: 1800

'

' --- Sample 2: Calculate negative difference ---

DTO.TimeDiff|09:00:00|10:00:00||$$DIF

MBX.The difference is $$DIF seconds. ' Result: -3600

'

' --- Sample 3: Using custom format ---

$$TAC = "15h 30m 15s"

$$TAD = "15h 30m 00s"

$$FMT = "Hh Mm Ss"

DTO.TimeDiff|$$TAC|$$TAD|$$FMT|$$DIF

MBX.Difference with custom format is $$DIF seconds. ' Result: 15

ENR.

 

Remarks

 

The result is always in total seconds, making it easy to use in further calculations. The command correctly handles time rollovers (e.g., crossing midnight), as it treats both times as being on the same calendar day.

 

Limitations:

 

Providing invalid time strings for either parameter will result in a parameter error.

 

See also:

 

    DTO. - Date Time Operations

    DTO.Date Diff

    DTO.Is Time Passed

    DTO.Add Seconds

    TTV. - Time to Value

    DTC. - Date and Time Calculation