Solved

Trying to format datediff Hours, Minutes, Seconds

Posted on 2004-08-19
6
728 Views
Last Modified: 2011-04-14
I track the date and time a user enters a site and leaves. I obtain the Hours, Minutes and Seconds with the following code:

            LoggedTimeHours = datediff("h", request.Cookies("TimeIn"), now())
            LoggedTimeMinutes = datediff("n", request.Cookies("TimeIn"), now())
            LoggedTimeSeconds = datediff("s", request.Cookies("TimeIn"), now())

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
Comment
Question by:fisherbrsnch00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 12

Expert Comment

by:kingsfan76
ID: 11848947
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

totalSec = datediff("S", request.Cookies("TimeIn"), now())

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

by:
Dexstar earned 125 total points
ID: 11848983
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

by:Tasneem
ID: 11849281
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:fisherbrsnch00
ID: 11849491
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

by:fisherbrsnch00
ID: 11849502
My mistake, it was Dexstar's code that I used. So thank you Dexstar.
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 11852213
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question