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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer Using DateDiff

Status
Not open for further replies.

Scarecrow

MIS
Mar 16, 2001
12
0
0
US
I am in the process of putting a countdown timer in my online auction. Right now, I'm using the DateDiff function to figure the Hours, Minutes, and Seconds remaining in the auction. The 2 problems I'm facing are: (1)How do I format the minues and seconds to represent 'real' minutes and seconds, not the total minutes and seconds; and (2)Where is the best place to format the number to whole numbers instead of having decimals?

The script I created is here:

DiffHrs = DateDiff("h", Now, rsBid("LastBidTime"))
DiffMin = DateDiff("n", Now, rsBid("LastBidTime"))
Diffsec = DateDiff("s", Now, rsBid("LastBidTime"))
Response.Write _
&quot;<TD><FONT SIZE=&quot;&quot;-1&quot;&quot;>&quot; & rsBid(&quot;MaxBidAmount&quot;) & &quot;</TD>&quot; & _
&quot;<TD><FONT SIZE=&quot;&quot;-1&quot;&quot;>&quot; & rsBid(&quot;LastBidTime&quot;) & &quot;</TD>&quot; & _
&quot;<TD><FONT SIZE=&quot;&quot;-1&quot;&quot;>&quot; & DiffHrs & &quot;:&quot; & DiffMin & &quot;:&quot; & Diffsec & &quot;</TD>&quot;

Also, if there's an earier way to do this, I'm open to suggestions.
 
Ok, I'm going to do some rewriting, but I will explain it as Igo along because it will be 5 times longer if I just explain it :)

Version 1
Code:
Dim DiffHrs DiffMins, DiffSec

'Ok, lets get the difference in seconds:
DiffSec = DateDiff(&quot;s&quot;,Now, rsBid(&quot;LastBidTime&quot;))

'Now what we are going to do is get the number of hours and eat the decimal
DiffHrs = Fix(DiffSec/3600)  'Fix() removes the decimal portion of a number and returns an integer

'Ok, so we have the hours, lets recalcularte how many seconds we have
DiffSec = DiffSec - DiffHrs * 3600

'Now lets get minutes
DiffMin = Fix(DiffSec/60)

'and fix the seconds again
DiffSec = DiffSec - DiffHrs * 60

'Tada! We should now have correct values for seonds, minutes, and hours

Vrsion 2
Code:
Dim DiffHrs DiffMins, DiffSec

'get numbers like you have above, except use fix to kill decimals
DiffHrs = Fix(DateDiff(&quot;h&quot;, Now, rsBid(&quot;LastBidTime&quot;)))
DiffMin = Fix(DateDiff(&quot;n&quot;, Now, rsBid(&quot;LastBidTime&quot;)))
Diffsec = Fix(DateDiff(&quot;s&quot;, Now, rsBid(&quot;LastBidTime&quot;)))

'now we need to correct the seconds and minutes:
DiffMin = DiffMin - DiffHrs * 3600
Diffsec = DiffSec - DiffHrs * 3600 - DiffMin * 60

'And tada again :) All fixed

Hope this helped,
-Tarwn

[/code] Experts are only people who have realized how much they will never know about a language.
________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Thanks for the help. I had to make a couple of corrections, though. Here is the corrected script in case anyone else needs it:

Dim DiffHrs, DiffMin, DiffSec

DiffSec = DateDiff(&quot;s&quot;,Now, rsBid(&quot;LastBidTime&quot;))
DiffHrs = Fix(DiffSec/3600)
DiffSec = DiffSec - DiffHrs * 3600
DiffMin = Fix(DiffSec/60)
DiffSec = DiffSec - DiffMin * 60

Response.Write _
&quot;<TD>&quot; & DiffHrs & &quot;:&quot; & DiffMin & &quot;:&quot; & Diffsec & &quot;</TD>&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top