Search Tools Links Login

DateDiffEx


Visual Basic 6, or VB Classic

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.

Original Author: Paul Mather

Code

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

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 104 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.