Solved

# Trying to format datediff Hours, Minutes, Seconds

Posted on 2004-08-19
723 Views
I track the date and time a user enters a site and leaves. I obtain the Hours, Minutes and Seconds with the following code:

It gives me results such as... 0 : 4 : 190 (Hours:Minutes:Seconds).

For longer Times I've also Noticed that the minutes can be in the hundreds and the seconds in the thousands.

I'm guessing that this is because what it is giving is the TOTAL number of hours/minutes/seconds between the two dates.

Does anyone have a reliable routine to break down the three numbers to show the time that has passed, (ie. 1 Hours : 2 Minutes : 45 Seconds), instead of the total number of each?
Thanks
0
Question by:fisherbrsnch00

LVL 12

Expert Comment

here's a link to a post that i answered that does almost exactly the same thing.

you probably only need to change the line

totalSec = datediff("S",CurrentDate,EndDate)

to

and you might want to take out the day part if you don't want to display number of days
if you have any further question on the codes i'll be glad to help
0

LVL 19

Accepted Solution

Dexstar earned 125 total points
fisherbrsnch00:
> Does anyone have a reliable routine to break down the three numbers to
> show the time that has passed, (ie. 1 Hours : 2 Minutes : 45 Seconds),
> instead of the total number of each?

Try this:

const cSecondsPerDay         = 86400    ' 60*60*24
const cSecondsPerHour       = 3600      ' 60*60
const cSecondsPerMinute    = 60           ' Duh

Function GetCountdownString( dtTarget )
Dim dtTemp
Dim nDays
Dim nHours
Dim nMinutes
Dim nSeconds
Dim strUnits

On Error Resume Next
dtTemp = CDate(dtTarget)
If Err.Number <> 0 Then
GetCountdownString = "Invalid Date"
Else
If dtTemp > Now() Then
nSeconds = DateDiff("s", Now(), dtTemp)
Else
nSeconds = DateDiff("s", dtTemp, Now())
End If

' Subtracting 0.5 forces it to truncate instead of round
nDays        = CInt(nSeconds / cSecondsPerDay - 0.5)
nSeconds    = nSeconds MOD cSecondsPerDay
nHours        = CInt(nSeconds / cSecondsPerHour - 0.5)
nSeconds    = nSeconds MOD cSecondsPerHour
nMinutes    = CInt(nSeconds / cSecondsPerMinute - 0.5)
nSeconds    = nSeconds MOD cSecondsPerMinute

GetCountdownString = ""

If nDays = 1 Then
strUnits = "Day"
Else
strUnits = "Days"
End If
GetCountdownString = GetCountdownString & nDays & " " & strUnits & ", "

If nHours = 1 Then
strUnits = "Hour"
Else
strUnits = "Hours"
End If
GetCountdownString = GetCountdownString & nHours & " " & strUnits & ", "

If nMinutes = 1 Then
strUnits = "Minute"
Else
strUnits = "Minutes"
End If
GetCountdownString = GetCountdownString & nMinutes & " " & strUnits & ", "

If nSeconds = 1 Then
strUnits = "Second"
Else
strUnits = "Seconds"
End If
GetCountdownString = GetCountdownString & nSeconds & " " & strUnits
End If
End Function

Then if you want to get the total time, just pass in Request.Cookies("TimeIn") as a parameter to that function, and it will return a string that gives you the result.  You may want to tweak the function a little to get the exact format you want.  I'll help with that if you aren't sure what to do.  Just tell me what you want it to look like.

HTH,
Dex*
0

LVL 4

Expert Comment

0

Author Comment

kingsfan76, thanks, that worked great.

Tasneem, thanks for the post, but I've already implemented kingsfan76's code and I prefer doing these calculations on the server side.
0

Author Comment

My mistake, it was Dexstar's code that I used. So thank you Dexstar.
0

LVL 19

Expert Comment

fisherbrsnch00:

> My mistake, it was Dexstar's code that I used. So thank you Dexstar.

You're welcome.  Glad you got it working.  :)

-D*
0

## Featured Post

### Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …