Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time elapsed in format of hh:mm:ss

Status
Not open for further replies.

cluM09

Technical User
May 15, 2004
127
US
I am writing a script that requires to determine how long the duration between two date/time. The date/time format is as 11/15/2006 9:08:23 PM to the date/time returned by the Now function.

I tried the DateDiff function as follows:

dtmStartTime = "11/02/2006 9:08:23 PM"
strElapsedTime = DateDiff("s", dtmStartTime, Now)

This only returns seconds.

The parameters are: "h" = hours; "n" = minutes; "s" = seconds.

Therefore, the DateDiff function only returns hours, minutes or seconds separately and never combines them into hh:mm:ss format.

Is there any way to do that in VBScript?

Thanks in advance!

CluM09
 
cluM09,

I normally do vb6 and not VbScript so bear with me. However;

dtmStartTime should (within its Variant wrapper) be a Double expressed in Days.

If you then have a variable setup called dtmEndTime and do

MsgBox Format$(dtmEndTime - dtmStartTime,"hh:mm:ss")

you should have a duration reported. It gets a little more complicated if dtmEndTime - dtmStartTime exceeds 1 day.

Alternatively determine the number of days, hours, minutes and seconds by dividing up the number of seconds you already have.

HTH Hugh,

 
Unfortunately VBScript does not support the Format function. I wish it would because it would make life alot easier.

Swi
 
VBScript does not support the Format function
You may try something like this:
FormatDateTime(yourTime, 4) ' 4=vbShortTime

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
OOps, sorry for the typo.
VBScript does not support the Format function
You may try something like this:
FormatDateTime(yourTime, 3) ' 3=vbLongTime

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Swi,
Drat!

PHV,
So I guess it could be;
MsgBox FormatDateTime(dtmEndTime - dtmStartTime, 3)

Regards Hugh,
 
Thank you all for the response!

HughLerwill,
I tried your suggestion, but it did not work. Below is my code:

dtmStartTime = "03/13/2006 02:10:20 AM"
dtmEndTime = Now
strUpTime = FormatDateTime(dtmEndTime - dtmStartTime, 3)
Wscript.Echo strUpTime

I got a runtime error at line 3 above. We still need to deal with the DateDiff function to get the time difference, but again we are stuck with the limitation as I indicated originally.

I also tried Swi's suggestions. It worked, but when the number of hours is more than 24, I got a problem with converting hours into days, months, and years, etc. Both codes work the same way.

Below are the codes:

Code 1:
dtmStartTime = "03/15/2006 09:00:20 PM"
dtmEndTime = Now

strUpTime = DateDiff("s", dtmStartTime, dtmEndTime)
Wscript.Echo convertTime(strUpTime)

Function convertTime(seconds)
ConvSec = seconds Mod 60
If Len(ConvSec) = 1 Then
ConvSec = "0" & ConvSec
End If
ConvMin = (seconds Mod 3600) \ 60
If Len(ConvMin) = 1 Then
ConvMin = "0" & ConvMin
End If
ConvHour = seconds \ 3600
If Len(ConvHour) = 1 Then
ConvHour = "0" & ConvHour
End If
convertTime = ConvHour & ":" & ConvMin & ":" & ConvSec
End Function

Code 2:
d1 = "01/15/2006 09:00:20 PM"
d2 = Now

Wscript.Echo TimeSpan(d1, d2)

Function TimeSpan(dt1, dt2)
If (isDate(dt1) And IsDate(dt2)) = false Then
TimeSpan = "00:00:00"
Exit Function
End If
seconds = Abs(DateDiff("S", dt1, dt2))
minutes = seconds \ 60
hours = minutes \ 60
minutes = minutes mod 60
seconds = seconds mod 60
If Len(hours) = 1 Then hours = "0" & hours
TimeSpan = hours & ":" & _
Right("00" & minutes, 2) & ":" & _
Right("00" & seconds, 2)
End Function

If we can further convert the hours into days, months, and years based on the incremental numbers, we will be in business.

Thanks again!

CluM09
 
Swi,

Thank you for the help!

The script from the link has the vba function to convert seconds into seconds, minutes, hours, days, months, and years. However, this function DOES NOT work in VBscript.

The closest I can do from the code 1 that I posted above is to convert the hours into days when the hours reach 24 or more. Below is the code.

dtmStartTime = "03/15/2006 09:00:20 PM"
dtmEndTime = Now

strUpTime = DateDiff("s", dtmStartTime, dtmEndTime)
Wscript.Echo convertTime(strUpTime)

Private Function convertTime(seconds)
Dim ConvSec, ConvMin, ConvHour, ConvDay, strDay
ConvSec = seconds Mod 60
If Len(ConvSec) = 1 Then
ConvSec = "0" & ConvSec
End If
ConvMin = (seconds Mod 3600) \ 60
If Len(ConvMin) = 1 Then
ConvMin = "0" & ConvMin
End If
ConvHour = seconds \ 3600
If Len(ConvHour) = 1 Then
ConvHour = "0" & ConvHour
End If
If ConvHour = 24 Then
ConvHour = 00
convertTime = "1 Day, " & ConvHour & ":" & ConvMin & ":" & ConvSec
ElseIf ConvHour > 24 Then
ConvDay = ConvHour \ 24
ConvHour = ConvHour Mod 24
If Len(ConvHour) = 1 Then ConvHour = "0" & ConvHour
If ConvDay = 1 Then
strDay = " Day, "
Else
strDay = " Days, "
End If
convertTime = ConvDay & strDay & ConvHour & ":" & ConvMin & ":" & ConvSec
Else
strDay = "0 Day, "
convertTime = strDay & ConvHour & ":" & ConvMin & ":" & ConvSec
End If
End Function

Thanks again!

CluM09
 
I took the post from freevbcode and coverted it to VB script and it worked fine.

Swi
 
Here it is. Note that this code is straight from the module of the VB code in the link I provided.

Code:
dtmStartTime = "03/15/2006 09:00:20 PM"
dtmEndTime = Now

strUpTime = DateDiff("s", dtmStartTime, dtmEndTime)

MsgBox ConvTime(strUpTime, 5, True, True, ":")

Function ConvTime(Seconds, Level, fullreturn, _
   display_text, delimiter)

'Seconds - The seconds to be converted
'Level - up to what detail should seconds be broken to.
'Values:
'0 = Seconds 2 = Hours 4 = Weeks
'1 = Minutes 3 = Days  5 = Years
'Fullreturn - should the function return all levels e.g. 4 days, 13 hours,
    '34 minutes, 56 seconds
'or should just give the top level requested e.g. 4 days.
'display_text - should there be text in the returned value?
  'This adds "years", "weeks" and etc. after each value returned.
'delimiter - if display_text is set to false, then use the following delimiter
   'to separate values.


If ((Seconds > 2147483647) Or (Level > 5) Or _
   (Len(delimiter) > 1)) Then
ConvTime = 0
Exit Function
End If

Dim Minutes, Hours, Days
Dim Weeks, Years

Dim Minutes_, Hours_, Days_
Dim Weeks_, Years_

Minutes = Seconds \ 60
Hours = Minutes \ 60
Days = Hours \ 24
Weeks = Days \ 7
Years = Days \ 365

Seconds_ = Seconds - (60 * Minutes)
Minutes_ = Minutes - (60 * Hours)
Hours_ = Hours - (24 * Days)
Days_ = Days - (7 * Weeks)
Weeks_ = Weeks - (52 * Years)


If (fullreturn = True) Then

    Select Case Level
    
    Case 0
        If (display_text = True) Then
        ConvTime = Seconds & " seconds."
        Else
        ConvTime = Seconds
        End If
    Case 1
        If (display_text = True) Then
        ConvTime = Minutes & " minutes, " & Seconds_ & " seconds"
        Else
        ConvTime = Minutes & delimiter & Seconds_
        End If
    Case 2
        If (display_text = True) Then
        ConvTime = Hours & " hours, " & Minutes_ & " minutes, " _
           & Seconds_ & " seconds"
        Else
        ConvTime = Hours & delimiter & Minutes_ & delimiter & Seconds_
        End If
    Case 3
        If (display_text = True) Then
        ConvTime = Days & " days, " & Hours_ & " hours, " & _
           Minutes_ & " minutes, " & Seconds_ & " seconds"
        Else
        ConvTime = Days & delimiter & Hours_ & delimiter & _
             Minutes_ & delimiter & Seconds_
        End If
    Case 4
        If (display_text = True) Then
        ConvTime = Weeks & " weeks, " & Days_ & " days, " & Hours_ & _
               " hours, " & Minutes_ & " minutes, " & Seconds_ & " seconds"
        Else
        ConvTime = Weeks & delimiter & Days_ & delimiter & Hours_ & _
           delimiter & Minutes_ & delimiter & Seconds_
        End If
    Case 5
        If (display_text = True) Then
        ConvTime = Years & " years, " & Weeks_ & " weeks, " & Days_ & " days, " & Hours_ & " hours, " & Minutes_ & " minutes, " & Seconds_ & " seconds"
        Else
        ConvTime = Years & delimiter & Weeks_ & delimiter & Days_ & delimiter & Hours_ & delimiter & Minutes_ & delimiter & Seconds_
        End If
    Case Else
            ConvTime = 0
    End Select

Else

    Select Case Level
    
    Case 0
        If (display_text = True) Then
        ConvTime = Seconds & " seconds."
        Else
        ConvTime = Seconds
        End If
    Case 1
        If (display_text = True) Then
        ConvTime = Minutes & " minutes."
        Else
        ConvTime = Minutes
        End If
    Case 2
        If (display_text = True) Then
        ConvTime = Hours & " hours."
        Else
        ConvTime = Hours
        End If
    Case 3
        If (display_text = True) Then
        ConvTime = Days & " days."
        Else
        ConvTime = Days
        End If
    Case 4
        If (display_text = True) Then
        ConvTime = Weeks & " weeks."
        Else
        ConvTime = Weeks
        End If
    Case 5
        If (display_text = True) Then
        ConvTime = Years & " years."
        Else
        ConvTime = Years
        End If
    Case Else
            ConvTime = 0
    End Select

End If

End Function

Swi
 
Swi,

Thank you for the response!

Yes, your code works fine. At the same time, I also rewrote my previous code to convert days into months, and years, but I did not convert days into weeks first. However, I think your code is more robust than mine. Also, the things that I did not take into account are the leap year (where year/4 = even number) for February, the number of days for certain months, such as January has 31 days whereas April has 30 days, and so forth. Does your function take leap year and the number of days for certain months into account? Below is my code.

dtmStartTime = "03/15/2006 09:00:20 PM"
dtmEndTime = Now

strUpTime = DateDiff("s", dtmStartTime, dtmEndTime)
Wscript.Echo convertTime(strUpTime)

Private Function convertTime(seconds)
Dim ConvSec, ConvMin, ConvHour, ConvDay, strDay
ConvSec = seconds Mod 60
If Len(ConvSec) = 1 Then
ConvSec = "0" & ConvSec
End If
ConvMin = (seconds Mod 3600) \ 60
If Len(ConvMin) = 1 Then
ConvMin = "0" & ConvMin
End If
ConvHour = seconds \ 3600
If Len(ConvHour) = 1 Then
ConvHour = "0" & ConvHour
End If
If ConvHour = 24 Then
ConvHour = 00
ConvDay = 1
strDay = " Day, "
ElseIf ConvHour > 24 Then
ConvDay = ConvHour \ 24
ConvHour = ConvHour Mod 24
If Len(ConvHour) = 1 Then ConvHour = "0" & ConvHour
If ConvDay = 1 Then
strDay = " Day, "
Else
strDay = " Days, "
End If
If ConvDay = 30 Then
ConvMon = 1
ConvDay = ""
strDay = ""
strMon = " Month, "
ElseIf ConvDay > 30 Then
ConvMon = ConvDay \ 30
ConvDay = ConvDay Mod 30
If ConvMon > 1 Then
strMon = " Months, "
Else
strMon = " Month, "
End If
If ConvMon = 12 Then
ConvYear = 1
ConvMon = ""
strMon = ""
strYear = " Year, "
ElseIf ConvMon > 12 Then
ConvYear = ConvMon \ 12
ConvMon = ConvMon Mod 12
If ConvMon = 0 Then
ConvMon = ""
strMon = ""
ElseIf ConvMon = 1 Then
strMon = " Month, "
Else
strMon = " Month2, "
End If
If ConvYear = 1 Then
strYear = " Year, "
Else
strYear = " Years, "
End If
End If
End If
convertTime = ConvYear & strYear & ConvMon & strMon & ConvDay & strDay _
& ConvHour & ":" & ConvMin & ":" & ConvSec
Else
convertTime = ConvHour & ":" & ConvMin & ":" & ConvSec
End If
End Function

Thanks again!

CluM09

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top