Rewrite a Module

J

Jen

I have a time Module that I was given. I want it only to produce Hours,
Minutes and Seconds.

Can anyone help me with rewriting it. I have tried Doug Steele's Diff2Dates
but it is not working as well as this module. I would love to use Doug's if
only I could get it to work.....

Example 1 Day 11 Hours 3 Minutes 2 Seconds would show
25 Hours 3 Minutes 2 Seconds.....

Thanks a Million.... (I have gone past crazy with this issue.)


Public Function ElapsedTimeString(dateTimeStart As Date, dateTimeEnd As
Date) As String
'**********************************************************************************************
' Function ElapsedTimeString(dateTimeStart As Date, dateTimeEnd As Date) As
String
' Returns the time elapsed between a starting Date/Time and an ending
' Date/Time formatted as a string that looks like this:
' "10 days, 20 hours, 30 minutes, 40 seconds".
'**********************************************************************************************

Dim interval As Double, str As String, days As Variant
Dim hours As String, Minutes As String, seconds As String

If IsNull(dateTimeStart) = True Or _
IsNull(dateTimeEnd) = True Then Exit Function

interval = dateTimeEnd - dateTimeStart

days = Fix(CSng(interval))
hours = Format(interval, "h")
Minutes = Format(interval, "n")
seconds = Format(interval, "s")

' Days part of the string
str = IIf(days = 0, "", _
IIf(days = 1, days & " Day", days & " Days"))
str = str & IIf(days = 0, "", _
IIf(hours & Minutes & seconds <> "000", ", ", " "))

' Hours part of the string
str = str & IIf(hours = "0", "", _
IIf(hours = "1", hours & " Hour", hours & " Hours"))
str = str & IIf(hours = "0", "", _
IIf(Minutes & seconds <> "00", ", ", " "))

' Minutes part of the string
str = str & IIf(Minutes = "0", "", _
IIf(Minutes = "1", Minutes & " Minute", Minutes & " Minutes"))
str = str & IIf(Minutes = "0", "", IIf(seconds <> "0", ", ", " "))

' Seconds part of the string
str = str & IIf(seconds = "0", "", _
IIf(seconds = "1", seconds & " Second", seconds & " Seconds"))
ElapsedTimeString = IIf(str = "", "0", str)

End Function
 
V

Van T. Dinh

Change your code to (untested & a bit unconsistent due to quick mods)

Public Function ElapsedTimeString(dateTimeStart As Date, dateTimeEnd As
Date) As String
'**********************************************************************************************
' Function ElapsedTimeString(dateTimeStart As Date, dateTimeEnd As Date) As
String
' Returns the time elapsed between a starting Date/Time and an ending
' Date/Time formatted as a string that looks like this:
' "10 days, 20 hours, 30 minutes, 40 seconds".
'**********************************************************************************************

Dim interval As Double, str As String, days As Integer
Dim hours As Integer, minutes As Integer, seconds As Integer

If IsNull(dateTimeStart) = True Or _
IsNull(dateTimeEnd) = True Then Exit Function

interval = dateTimeEnd - dateTimeStart

days = Fix(CDbl(interval))
hours = Hour(interval)
minutes = Minute(interval)
seconds = Second(interval)

hours = days * 24 + hours

' Hours part of the string
str = IIf(hours = 0, "", _
IIf(hours = 1, hours & " Hour", hours & " Hours"))

' Minutes part of the string
str = str & IIf(minutes = 0, "", _
IIf(minutes = 1, " " & minutes & " Minute", " " & minutes & "
Minutes"))

' Seconds part of the string
str = str & IIf(seconds = 0, "", _
IIf(seconds = 1, seconds & " Second", seconds & " Seconds"))

' Final string
ElapsedTimeString = IIf(str = "", "0 duration", str)

End Function
 
D

Douglas J. Steele

What problem did you have with the Diff2Dates module?

As a test, I checked the following in the Immediate Window:

?Diff2Dates("hns", #2006-10-06 10:00:00#, #2006-10-08 9:03:10#)
47 hours 3 minutes 10 seconds
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top