by Paul Mather (15 Submissions)
Category: Coding Standards
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (8 Votes)
Input a Starting Time and an Ending Time and this function will translate it into English. For Example: DateDiffEx("02/12/2002 5:05:28 AM","02/14/2002 8:00:58 PM") Return= "2 days, 15 hours", DateDiffEx("02/14/2002 5:05:28 AM","02/14/2002 8:00:58 PM") Return= "15 hours, 55 minutes, 30 seconds". It only includes the "s" if the value does not equal 1. It eliminates the 0 values as well.
Public Function DateDiffEx(StartTime As Date, EndTime As Date) As String
DateDiffEx = DateDiffExFormat(DateDiff("d", StartTime, EndTime) \ 365, "year")
DateDiffEx = DateDiffEx & DateDiffExFormat((DateDiff("s", StartTime, EndTime) \ 86400) _
Mod 365, "day")
DateDiffEx = DateDiffEx & DateDiffExFormat((DateDiff("s", StartTime, EndTime) \ 3600) _
Mod 24, "hour")
DateDiffEx = DateDiffEx & DateDiffExFormat((DateDiff("s", StartTime, EndTime) \ 60) _
Mod 60, "minute")
DateDiffEx = DateDiffEx & DateDiffExFormat(DateDiff("s", StartTime, EndTime) _
Mod 60, "second")
If Len(DateDiffEx) > 0 Then
DateDiffEx = Mid(DateDiffEx, 1, Len(DateDiffEx) - 2)
End If
End Function
Private Function DateDiffExFormat(inputValue As Long, unitValue As String) As String
If inputValue <> 0 Then
DateDiffExFormat = inputValue & " " & unitValue & IIf(inputValue <> 1, "s", "") & ", "
End If
End Function