Re-Writing Module


G

Guest

I have a module that was given to me to calculate time. It works
beautifully. However it shows Days. I need to see if someone could help
modify it to calculate total hours.

Example 1 day 11 hours 3 minutes 2 seconds
25 hours 3 minutes 2 seconds

I have tried to use Dave Steeles Diff2Date but I have not be successful. I
have about 6 forms and 20 plus reports that us the module and it would be
easier to modify the module than change all the others.

I am truly grateful for all ya'll do...Thanks Jen

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
 
Ad

Advertisements

C

Cinzia

Hi Jen
Jen said:
I have a module that was given to me to calculate time. It works
beautifully. However it shows Days. I need to see if someone could
help modify it to calculate total hours.

Example 1 day 11 hours 3 minutes 2 seconds
25 hours 3 minutes 2 seconds

Perhaps 35 hours 3 minutes 2 seconds
I have tried to use Dave Steeles Diff2Date but I have not be
successful. I have about 6 forms and 20 plus reports that us the
module and it would be easier to modify the module than change all
the others.

I am truly grateful for all ya'll do...Thanks Jen

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")

Add here:

hours = hours + days * 24
days = 0

' 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

Bye
 
Ad

Advertisements

D

Douglas J. Steele

Jen: you'd already posted this in another newsgroup, and there was a working
reply in that newsgroup long before you reposted to this newsgroup!
 

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