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

Subtracting Times 2

Status
Not open for further replies.

369852

Programmer
May 7, 2002
38
0
0
IE
Hi all,
I have a slight problem when i am subtracting times in ASP. At the moment i am using DateDiff but this only gives me the Hours e.g if i subtract 17:00 - 09:00 i get 8 which is grand but if the times happen to be 17:30 - 09:00 i still get 8 whereas i want to get 8:30.
Basically i can only get the hours. Can someone help me get hours, minutes and seconds!!
Thanks a million
Hope i haven't confused you!
:)
 
dateDiff(interval, date1, date2)

interval-->value
"yyyy" --> Year
"q" --> Quarter
"m" --> Month
"y" --> Day Of year
"d" --> Day
"w" --> Weekday
"ww" --> Week
"h" --> Hour
"n" --> Minute
"s" --> Second Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
You need to convert the expressions to seconds and subtract these values. After that you need to convert the result back to hh:mm:ss.
 

Thanks for the replies!

How will i convert it to seconds?
 
if the value is stored in variable with the subtype time it is easy:

seconds1 = datediff("s",#00:00:00#,timeVariable1)

Otherwise you need to extract the hours/minutes/seconds and calculate the total number of seconds.

How are the variables defined?
 
rightNow = now()

numHours = datediff("h",'1/30/02 6:25 AM',rightNow)
numMins = datediff("n",'1/30/02 6:25 AM',rightNow) - (60 * numHrs)
numSecs = datediff("s",'1/30/02 6:25 AM',rightNow) - (60 * numHrs) - (60 * numMins)


if numMins < 10 then numMins = &quot;0&quot; & numMins
if numSecs < 10 then numSecs = &quot;0&quot; & numSecs

response.write numHours &&quot;:&quot;& numMins &&quot;:&quot;& numSecs


Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 

I have it stored in the database as datetime, so i can just use the datediff to get the seconds? Then subtract and then convert back and display?!?
 
However you want to do it...

totalSecs = objRS(&quot;numSecs&quot;)
numHours = 3600 mod totalSecs
numMins = 60 mod (totalSecs - (3600 * numHours))
numSecs = totalSecs - (3600 * numHours) - (60 * numMins)

if numMins < 10 then numMins = &quot;0&quot; & numMins
if numSecs < 10 then numSecs = &quot;0&quot; & numSecs

response.write numHours &&quot;:&quot;& numMins &&quot;:&quot;& numSecs Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 

Thanks a million to everyone, used mwolf00 code, but thanks to everyone
 
Hmm, that looked vaguely familiar ;)

Here's another method to make up for the fact that I am not allowed to connect during the day any more and feel left out :)
Code:
Dim diff
diff = TarwnsMagicFunction(date1,date2)
Response.Write diff

:)

-Tarwn ________________________________________________________________________________
Sometimes it is how you ask the question: faq333-2924
Many ASP questions have already been answered, please check faq333-3048 and use the search tool before posting
 
I tried this and it didn't work. is this a built in function or something?

Dim diff
diff = TarwnsMagicFunction(date1,date2)
Response.Write diff


[lol][lol][rofl] _______________________________________________
[sub]{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }[/sub]
_______________________________________________
for the best results to your questions: FAQ333-2924
Is your question a
 
You guys crack me up! Why aren't you on during the day anymore Tarwn? We need you here! Don't tell me that your work is more important than helping us! [wink] Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
Nah, I'm just not &quot;allowed&quot; to connect while at work... ________________________________________________________________________________
Sometimes it is how you ask the question: faq333-2924
Many ASP questions have already been answered, please check faq333-3048 and use the search tool before posting
 
Hey

Just a little thing with the code mwolf00 , if the first time is 09:12 and the second time is 12:15 i get a minus in the number of minutes?? How can i get around this?

Thanks again
 
Hmmm... can you post your code? I'm not sure where the negative is coming in. The db is always returning the number of seconds...


totalSecs = abs(objRS(&quot;numSecs&quot;))

will ensure a postive value.

I don't get why the minutes would end up negative.... Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 

Actually i think i have been looking at it wrong it seems to be displaying a strange string!
Here is the code i have:

For example if
Time1 = 09:12
Time2 = 20:10

numMins = datediff(&quot;n&quot;,Time1,Time2) - (60 * numHours)

Nummins returns the answer 0-2 which is very strange!
Any ideas
 
In your example, if numHours>10 your answer will be negative.
 
so is there anyway i can combat this?
 
Time1 = &quot;09:12:01&quot;
Time2 = &quot;20:10:02&quot;
totalSecs = datediff(&quot;s&quot;,Time1,Time2)
totalMins = Int(totalSecs/60)
numHours = Int(totalMins/60)
numMins = totalMins - (60 * numHours)
numSecs = totalSecs - (3600 * numHours) - (60 * numMins)
response.write(&quot;H: &quot;&numHours&&quot;M: &quot;&numMins&&quot;S: &quot;&numSecs)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top