Link to home
Start Free TrialLog in
Avatar of forrest321
forrest321

asked on

VBScript Time difference

I need a function that accepts two times (24 hour/military format) and returns the difference in those times.

All times will be formatted hhmm - no seconds or miliseconds are required.

Here is what I have currently:
test.asp:
<%
Response.Write(SubtractTimes("815","1000"))
'***********************
Function SubtractTimes(iStart, iEnd)
'***********************
      dim tStart, tEnd
      dim sDiff
      tStart = FormatDateTime(iStart,vbShortTime)
      tEnd = FormatDateTime(iEnd,vbShortTime)
      SubtractTimes = DateDiff(mi, DATEADD(mi,tEnd,0), DATEADD(mi,tStart,0))
End Function
%>

The output *should* be "145" however I am getting an error.

Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'tEnd'
/forrest/test.asp, line 10
Avatar of ellandrd
ellandrd
Flag of Ireland image

Try use the time difference function:

Syntax:
DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]])

Try like:

Private Sub Form_Load()
    Dim startdate As Date
    startdate = DateAdd("s", -50, Now())
    Dim enddate As Date
    enddate = Now()
    MsgBox DateDiff("s", startdate, enddate)
End Sub
Avatar of forrest321
forrest321

ASKER

I'm not sure if that will work for what I need.  This function is going to be called possibly hundreds of times as the page loads, using various times from a db.  The issue I am having is taking those arbitrary times, and calculating the amount of time between them.

The times are being stored as 4 digit integers, which need to be converted to dateless times and then subtracted.  Or, if it is easier, they can be set to have the same date and then subtracted.
This should do what you want, as long as the four digit integers are maintained, although you will need to convert them to a string before you can then convert them to DateTime.

        sTime1 = "0815"
        sTime2 = "1000"

        Dim Time1 As Date
        Dim Time2 As Date

        Dim Culture As New System.Globalization.CultureInfo("en-US", True)

        Time1 = Date.ParseExact(sTime1, "hhmm", Culture)
        Time2 = Date.ParseExact(sTime2, "hhmm", Culture)

        Dim TimeDiff As System.TimeSpan
        TimeDiff = Time2.Subtract(Time1)

        MsgBox(TimeDiff.ToString)


The only things you need to input are sTime1 and sTime2 which are string values. TimeDiff represents the calculated difference between the times and is a DateTime variable (if you need to use it for further manipulation) but upon conversion to a string at the end comes up with "01:45:00" although this of course can be modified depending on what you want to display. For example:

MsgBox(TimeDiff.Hours & TimeDiff.Minutes)

Gives "145" as the output.


Let me know how it goes,

James
Just a reminder, for the function to work correctly the leading zero must be maintained for the time to be calculated correctly, as in "0845" not "845"
ASKER CERTIFIED SOLUTION
Avatar of JesterToo
JesterToo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<%
Response.Write(SubtractTimes("815","1000"))
'***********************
Function SubtractTimes(iStart, iEnd)
'***********************    
dim tStart,tEnd
tStart=left(cstr(iStart),len(cstr(iStart))-2) & ":" & Right(iStart,2)
tEnd = left(cstr(iEnd),len(cstr(iEnd))-2) & ":" & Right(iEnd,2)
SubtractTimes = DateDiff("n",cdate(tStart),cdate(tEnd))
End Function%>

This should work even if you don't have the leading zro on the time
naiea1231
  That appears to be .NET.  I am using VBScript/ASP classic....but believe me I would rather be using .NET.

JohnHorb
  That function returned 105.

JesterToo
  That function seems to work perfectly.

Thanks for all the help!
You're welcome!  and thanks for the grade!

-- Lynn
Ok, I'm getting some odd results here...

1500 - 700 = -800

200 - 0 = -200

-800 - -200 = 0600

1500 - 700 = -800

Why is it returning negative results?
Also, if you can modify it to return results as the number of hours with minutes as a decimal number, I'll give you another 100 points.

i.e, I would like 5 1/2 hours to return as 5.5
The negative results are because the beginning time is greater than the ending time... if you need to measure time elapsed between a time from one date until a time on a later date then the dates will need to be passed in additon to the times and a small bit more logic would need to be added to account for the dates.

Let me know if that is acceptable (in other words, you can provide the dates to the function call).  Changing the return values to be a decimal rather than HHMM is really easy.  I'll do that first.  Do you want the result as a numeric or string?

-- Lynn
The negative results were an error on my part...

For the other part, I don't think we will need to factor in dates....just the decimal value as a string should finish it up for me.  How do I give you more points?  Will I need to open a new question?

Thanks for all the help!
No need for extra points :)

I'll post a modified script a bit later today... currently have an issue I'm working on.

-- Lynn
Hedre is the modified script.  It returns string results in this pattern... HH.HH

I.E., 2 hours and 20 minutes would be returned as 01.33

If you would prefer to omit the leading "0" change the commented line near the end of the script.




'******************************************
Function SubtractTimes(iStart, iEnd)
'******************************************
' Given the following input values...
'    @TimBeg = 0815
'    @TimEnd = 1000
' the result should be 01.75
'******************************************

Dim BegTime  ' Beginning time converted to an integer
Dim EndTime  ' Ending    time converted to an integer
Dim BH       ' Beginning hour converted to minutes since midnight
Dim BM       ' Beginning mins
Dim BT       ' Beginning time in minutes since midnight
Dim EH       ' Ending hour converted to minutes since midnight
Dim EM       ' Ending mins
Dim ET       ' Ending time in minutes since midnight
Dim TM       ' Total minutes in interval
Dim IH       ' Interval hours
Dim IM       ' Interval mins
Dim ID       ' Interval decimal portion of hour representing IM
Dim IT       ' interval time expressed as HH.HH

   BegTime = Cint(iStart)
   EndTime = Cint(iEnd)

   BH = (BegTime \ 100) * 60
   BM = BegTime - ((BegTime \ 100) * 100)
   BT = BH + BM

   EH = (EndTime \ 100) * 60
   EM = EndTime - ((EndTime \ 100) * 100)
   ET = EH + EM

   TM = ET - BT

   IH = TM \ 60
   IM = TM Mod 60
   ID = Cstr(Round(IM / 60, 2))

   If Left(ID,1) = "0" THen
      ID = Mid(ID,2)
   End If
   IT = Right("00" & IH,2) & ID     ' use this for return format of:  02.33
 ' IT = IH & ID                            ' use this for return format of:  2.33

   SubtractTimes = IT

End Function              


-- Lynn
Cool, Thanks!