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

Time Calculations

Status
Not open for further replies.

Delphin

Programmer
Nov 27, 2001
134
US
I have written a function in VB calculating the difference in times elasped based on a 24 hour clock. It returns the number of hours in decimal form such as 1.5 for 1 1/2 hours. Now due to limitations with a phone integration system I need to be able to write something in VB Script. I am having problems converting the VB funtion, so if anyone can help me it would be appreciated.
 
Here is the function I am using in VB

Public Function ElapsedTime(tStart, tStop) As String

Dim dtr, dtl, jml As Long
Dim time
dtl = (Hour(tStart) * 3600) + (Minute(tStart) * 60)
dtr = (Hour(tStop) * 3600) + (Minute(tStop) * 60)
If tStop < tStart Then
jml = 86400
Else
jml = 0
End If
jml = jml + (dtr - dtl)
time = Format(Str(Int((Int((jml / 60)) Mod 60))), &quot;00&quot;)
time = time / 60
time = Format(time, &quot;.00&quot;)
ElapsedTime = Format(Str(Int((Int((jml / 3600)) Mod 24))), &quot;00&quot;) + time
End Function
 
VBScript is not very strict about types. So when defining a function or variable you do not need to use keyword As String (Integer, and so on). In your code you can comment them.
Functions as Str, Int have their equvalents in VBScript as CStr, CInt.
As for Format function, there are 4 analogical functions: FormatNumber, FormatDateTime, FormatCurrency, FormatPercent.
I would suggest in your case to use only FormatNumber(expression,2) - it will format expression with 2 digits after decimal dot.
I checked in VB values of variable time - they were only different formats 00,0,.00. So in VBScript may be you will not need this variable.
I would suggest to check
for definitions a nd examples of use of functions, operators, etc. in VBScript.
Wish you success!
 
Delphin,

Sorry about th edelay in resonding.

The DateDiff function can be used in VBScript as well as VB/VBA

The syntax is DateDiff(interval, Earliest date/time, Latest date/time)

It returns the interval between two dates or times. The result is dependent on the Interval argument passed to it. Time intervals are &quot;h&quot; for Hours, &quot;n&quot; for Minutes and &quot;s&quot; for Seconds (&quot;m&quot; is used for Months). As it returns only a whole number if you want fractional times in hours , use &quot;n&quot; for minutes and divide by 60. The following example will give you an idea.
Code:
Dim dtNow, dtThen
Dim Elapsed
    dtNow = &quot;12:30:00&quot;
    dtThen = &quot;09:00:00&quot;
    Elapsed = DateDiff(&quot;n&quot;, dtThen, dtNow) / 60
    MsgBox Elapsed
A.C.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top