Solved

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

Posted on 2008-10-23
11
386 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:mattboy_slim
[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
  • 2
  • 2
  • +1
11 Comments
 
LVL 16

Expert Comment

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

http://www.w3schools.com/vbScript/func_datediff.asp
0
 
LVL 2

Author Comment

by:mattboy_slim
ID: 22789678
I've looked at that. I'm looking for a more specific answer.
Thanks,
matt
0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 22789688
Is this what your after?

Response.Write DateDiff("h", "01/01/1970 00:00:00", "01/01/1970 01:00:00")
0
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!

 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 250 total points
ID: 22789700
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
 
LVL 2

Author Comment

by:mattboy_slim
ID: 22789721
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
 
LVL 2

Author Comment

by:mattboy_slim
ID: 22789725
Morcalavin, your answer only gave the number of hours, not hours with decimal places. Thanks for the reply though.
0
 
LVL 16

Expert Comment

by:brad2575
ID: 22789731
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
 
LVL 2

Author Closing Comment

by:mattboy_slim
ID: 31509364
Thanks again, your code worked flawlessly.
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22789761
to trim to 2 decimal places, see the formatnumber function.
http://www.w3schools.com/vbScript/func_formatnumber.asp
0
 
LVL 2

Author Comment

by:mattboy_slim
ID: 22791020
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
 
LVL 2

Author Comment

by:mattboy_slim
ID: 22791065
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

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 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