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
349 Views
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.

Matt
0
Question by:mattboy_slim
• 6
• 2
• 2
• +1

LVL 16

Expert Comment

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

I've looked at that. I'm looking for a more specific answer.
Thanks,
matt
0

LVL 18

Expert Comment

Response.Write DateDiff("h", "01/01/1970 00:00:00", "01/01/1970 01:00:00")
0

LVL 32

Accepted Solution

Daniel Wilson earned 250 total points
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
``````
0

LVL 2

Author Comment

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

Morcalavin, your answer only gave the number of hours, not hours with decimal places. Thanks for the reply though.
0

LVL 16

Expert Comment

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

Thanks again, your code worked flawlessly.
0

LVL 32

Expert Comment

to trim to 2 decimal places, see the formatnumber function.
http://www.w3schools.com/vbScript/func_formatnumber.asp
0

LVL 2

Author Comment

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))

%>
``````
0

LVL 2

Author Comment

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)

%>
``````
0

Featured Post

Suggested Solutions

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…