ASP Math: Get amount of time between two values (such as 10/23/2008 2:30:00 and 10/23/2008 4:30:00)?

Using Classic ASP, I need to get the amount of time (in hours) between two values:
For example:
 - 10/23/2008 2:00:00 to 10/23/2008 2:20:00 = 0.33
 - 10/23/2008 7:45:00 to 10/23/2008 11:30:00 = 3.75

I've researched 'datediff' and 'dateadd' with no luck.  I cannot seem to find any examples of 'datediff' using the time, only the date, but apparently you get get number of seconds/minutes/hours.

Thanks in advance,
Matt
LVL 2
mattboy_slimAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brad2575Commented:
this displays all the different functions (you can go by hours, minutes, seconds, etc)

http://www.w3schools.com/vbScript/func_datediff.asp
0
mattboy_slimAuthor Commented:
I've looked at that. I'm looking for a more specific answer.
Thanks,
matt
0
MorcalavinCommented:
Is this what your after?

Response.Write DateDiff("h", "01/01/1970 00:00:00", "01/01/1970 01:00:00")
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Daniel WilsonCommented:
http://www.w3schools.com/vbScript/func_datediff.asp



dim iNumSecs
dim dHoursDiff
 
iNumSecs = datediff("s", "10/23/2008 2:00:00", " 10/23/2008 2:20:00")
dHoursDiff = cdbl(iNumSecs / 3600.0)
 
response.write dHoursDiff

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mattboy_slimAuthor Commented:
Daniel, that is exactly what I wanted, and it works perfectly. Now I just need to trim it to 2 decimal places.

Thanks!
Matt
0
mattboy_slimAuthor Commented:
Morcalavin, your answer only gave the number of hours, not hours with decimal places. Thanks for the reply though.
0
brad2575Commented:
if you want to convert something to a time in minutes, etc, you would take the datediff useing the seconds and then do some math on it to get minutes, hours, etc.

So you would take
 this will give you ONLY the seconds left (remainder from getting minuts)
Seconds = DateDiff("s", "01/01/1970 00:00:00", "01/01/1970 01:00:00")/60 - DateDiff("m", "01/01/1970 00:00:00", "01/01/1970 01:00:00")

 this will give you ONLY minutes left (remainder from getting hours)
Minutes = DateDiff("h", "01/01/1970 00:00:00", "01/01/1970 01:00:00")/60 - DateDiff("m", "01/01/1970 00:00:00", "01/01/1970 01:00:00")

 this will give you ONLy hours left (remainder from getting days
Hours = DateDiff("d", "01/01/1970 00:00:00", "01/01/1970 01:00:00")/24 - DateDiff("h", "01/01/1970 00:00:00", "01/01/1970 01:00:00")

-- so on
0
mattboy_slimAuthor Commented:
Thanks again, your code worked flawlessly.
0
Daniel WilsonCommented:
to trim to 2 decimal places, see the formatnumber function.
http://www.w3schools.com/vbScript/func_formatnumber.asp
0
mattboy_slimAuthor Commented:
Thanks again Daniel, it works great.  Here is the code that works for anyone searching in the future:
<%    
dim var_DateDiffSeconds, var_DateDiffHours, var_datefirst, var_datenext
var_datefirst = request.querystring("varfirst")
var_datenext = request.querystring("varnext")
 
var_DateDiffSeconds = datediff("s", var_datefirst, var_datenext)
var_DateDiffHours = cdbl(var_DateDiffSeconds / 3600.0)
 
response.write var_DateDiffHours & "<br/>"
response.write("Hours: " & FormatNumber(var_DateDiffHours, 2))
%>

Open in new window

0
mattboy_slimAuthor Commented:
I compressed it a little more:
<%    
dim var_DateDiffSeconds, var_DateDiffHours, var_datefirst, var_datenext
var_datefirst = request.querystring("varfirst")
var_datenext = request.querystring("varnext")
 
var_DateDiffTotal = FormatNumber(cdbl(datediff("s", var_datefirst, var_datenext) / 3600.0), 2)
 
response.write("Hours: " & var_DateDiffTotal)
%>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.