### Calculate Time between Times

'Returns time difference in NNhr NNmin.
`Sub TimeDiff(starttime As String, endtime As String) As String
Dim result As String
DateTime.TimeFormat="HHmm"
Dim st As Long = DateTime.TimeParse(starttime)
Dim et As Long = DateTime.TimeParse(endtime)
Dim td As Period = DateUtils.PeriodBetween(st,et)
result = td.Hours & "hr" & " " & td.Minutes & "min"
Return result
End Sub`
'Returns time difference in time format HHmm.
`Sub TimeDiffTime(starttime As String, endtime As String) As String
Dim result As String
DateTime.TimeFormat="HHmm"
Dim st As Long = DateTime.TimeParse(starttime)
Dim et As Long = DateTime.TimeParse(endtime)
Dim td As Period = DateUtils.PeriodBetween(st,et)
Dim h As String = td.Hours
If h < 10 Then h = "0" & h
Dim m As String = td.Minutes
If m < 10 Then m = "0" & m
result = h & m
Return result
End Sub`
'Returns time difference in minutes.
`Sub TimeDiffMin(starttime As String, endtime As String) As Int
Dim result As String
DateTime.TimeFormat="HHmm"
Dim st As Long = DateTime.TimeParse(starttime)
Dim et As Long = DateTime.TimeParse(endtime)
Dim td As Period = DateUtils.PeriodBetween(st,et)
result = (td.Hours * 60) + td.Minutes
Return result
End Sub`
`Sub buttonTest_Action
Log("TimeDiff between 2302 and 0601:" & CRLF & _
TimeDiff("0601", "2302") & CRLF & _
TimeDiffTime("0601", "2302") & CRLF & _
TimeDiffMin("0601", "2302") & "min")
End Sub`
Difference in seconds for time format HH:mm:ss
'Returns time difference in seconds for timeformat HH:mm:ss
'Example:
'Dim timestart As Long = DateTime.Now
'Do something like a delay for 5 seconds
'Log("Duration (s) = " & TimeDiffSec(DateTime.Time(timestart), DateTime.Time(DateTime.now)))
`Sub TimeDiffSec(starttime As String, endtime As String) As Int
Dim result As String
DateTime.TimeFormat="HH:mm:ss"
Dim st As Long = DateTime.TimeParse(starttime)
Dim et As Long = DateTime.TimeParse(endtime)
Dim td As Period = DateUtils.PeriodBetween(st,et)
result = (td.Hours * 60) + (td.Minutes * 60) + td.Seconds
Return result
End Sub`

Changed: , Category: DateTime, File: T6C183M1319.htm, ID: 1319