Calculate time intervals

 2000-02-04    Time    0    83

If you need to calculate the difference between two time values within a certain limit (e.g. when calculating elapsed work time), the worksheet function below can be useful since it makes it simple to perform the time interval calculation.

Function TimeInterval(StartTime As Double, EndTime As Double, LowerLimit As Double, UpperLimit As Double) As Double
' returns EndTime-StartTime limited by LowerLimit and UpperLimit
    TimeInterval = 0
    If StartTime > EndTime Then Exit Function
    If StartTime > UpperLimit Then Exit Function
    If EndTime < LowerLimit Then Exit Function
    If StartTime < LowerLimit Then StartTime = LowerLimit
    If EndTime > UpperLimit Then EndTime = UpperLimit
    TimeInterval = EndTime - StartTime
End Function
The examples below assumes that you have a start time in cell A1 and an end time in cell B1.

Return the elapsed time within 00:00 and 08:00 (0 = 00:00):

Return the elapsed time within 08:00 and 16:00:

Return the elapsed time within 16:00 and 24:00 (1 = 24:00):

Leave a comment:

Your comment will only be published after it has been moderated and found spam free.
Your e-mail address will only be used to display your Gravatar.