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

Calculating time 1

Status
Not open for further replies.

adamgme

IS-IT--Management
Sep 8, 2005
10
DK
Hi,

I have to calculate hours from given start and end time. For example:

Code:
Dim starttime, endtime, totaltime
starttime = "09:00"
endtime = "16:45"
' This is where i want to get the difference between above
totaltime = endtime - starttime

' The resulting value/string should be 07:45

Both times are always be in the same day, and 24 hour time format is used only with 15 minute interval. I need to calculate total hours of difference sessions, so the above coding will actually go in a "for loop" where different start/end times will be passed and the results of each will eb agregated in one variable, in the end total time spent should be produced.

Thanks alot for help :)

/Adam
 
You can use dateDiff for this and specify whether the results show minutes, seconds, hours etc...
 
use datediff() function

DateDiff("h",starttime, endtime)

-DNG
 
try this code and let me know if it works for you...

Code:
<%
Dim starttime, endtime, totaltime
starttime = "09:00"
endtime = "16:45"
totaltimehrs = DateDiff("h",starttime, endtime)
totaltimemins = DateDiff("n",starttime, endtime) mod 60
response.write totaltimehrs&":"&totaltimemins
%>

i am sure there are better ways of doing it...but just kept it simple so that you can get the point...

-DNG
 
I guess i need a little more help. :D What i am trying is to loop through different session times and get the total hours of all sessions. i guess i also need a way to add the hours :) please see below:

Code:
sub TotalHours(StempId)
	dim c1time, c2time, totaltime
	sessiontime = "00:00"
	totaltime = "00:00"
	for i = 0 to StempMaxRows
		if (StempArray(0,i) = StempId) Then
		Response.Write(StempArray(1,i) & "-" & StempArray(2,i) & "<br>")
		starttime = FormatDateTime(StempArray(1,i),4)
		endtime = FormatDateTime(StempArray(2,i),4)
		totaltimehrs = DateDiff("h",starttime, endtime)
		totaltimemins = DateDiff("n",starttime, endtime) mod 60
		sessiontime =  totaltimehrs&":"&totaltimemins
' HERE I NEED TO ADD THE HOURS
		totaltime = sessiontime + totaltime
		End if
	Next
	response.Write(totaltime)
end sub

Thanks alot in advance

/Adam
 
you mean...you looking for DateAdd()

you need to add what hours??

-DNG
 
I mean everytime when i come in this loop, i should get the time (end_time - start_time) and that time should be added in total_time. this loop will run from 1 to 5 times, every time there will be timings of different sessions. (9:00 to 16:00 or 9:30 to 15:15 etc...) what i need to do is to call this function to get the total hours spent for one group of sessions.
 
These are the class session for different course groups in our LMS systems, and i need to calculate the total hours of training provided for a given period of time. This function will be called for each course group containing 1 to many class sessions.
 
change this line
totaltime = sessiontime + totaltime
to

totaltime = DateAdd("h",sessiontime,totaltime)

that will result the total time in hours...if you want hours and mins separated then you can use the mod 60 thing
again as i used earlier....

-DNG
 
Hi,

I tried what you wrote, but i get an error. Here is the code:

Code:
sub TotalHours(StempId)
	dim sessiontime, totaltime
	sessiontime = "00:00"
	totaltime = "00:00"
	for i = 0 to StempMaxRows
		if (StempArray(0,i) = StempId) Then
		Response.Write(StempArray(1,i) & " - " & StempArray(2,i) & "<br>")
		starttime = FormatDateTime(StempArray(1,i),4)
		endtime = FormatDateTime(StempArray(2,i),4)
		totaltimehrs = DateDiff("h",starttime, endtime)
		totaltimemins = DateDiff("n",starttime, endtime) mod 60
		sessiontime =  FormatDateTime(totaltimehrs&":"&totaltimemins,4)
		totaltime = FormatDateTime(totaltime,4)
		totaltime = DateAdd("h",sessiontime,totaltime)
		End if
	Next
	response.Write("Time: " & totaltime)
end sub

And here is the error:

Code:
Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch: '[string: "07:00"]'
/reports/stats.asp, line 70

Line 70 is: totaltime = DateAdd("h",sessiontime,totaltime)

Any ideas?

Thanks in advance.

/Adam
 
You can't add to the totaltime after you have formatted it as a string. You'll have to accumulate the totaltime as number of minutes, and convert it to hours and minutes just before you print it. I also don't understand why you are calling FormatDateTime on an already formatted hh:mm string. I doubt it will do anything good.

Code:
sub TotalHours(StempId)
    dim sessiontime, totaltime
    sessiontime = "00:00"
[b]    totaltime = 0[/b]
    for i = 0 to StempMaxRows
        if (StempArray(0,i) = StempId) Then
        Response.Write(StempArray(1,i) & " - " & StempArray(2,i) & "<br>")
        starttime = FormatDateTime(StempArray(1,i),4)
        endtime = FormatDateTime(StempArray(2,i),4)
        totaltimehrs = DateDiff("h",starttime, endtime)
        totaltimemins = DateDiff("n",starttime, endtime) mod 60
[b]        sessiontime =  totaltimehrs & ":" & totaltimemins
        totaltime = totaltime + DateDiff("n",starttime,endtime)[/b]
        End if
    Next
[b]    response.Write("Time: " & (totaltime \ 60) & ":" & (totaltime mod 60)[/b]
end sub


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
First suggestion: I would suggest not using the DateDiff("h"... for this. Rounding is going to give you some strange results, plus creating a total time is going to be more complex.

Second Suggestion: If this data is coming from the database (the array looks likt GetRows() output), you may be able to increase execution time if you add your DateDiff to your SQL command and pull that back in your query

I see several solutions already above that will work, but figure I will add my own example in:
Code:
Sub DisplayTotalHours(STempId)
   Dim total_time, temp_time, row_ctr
   For row_ctr = 0 to UBound(STempArray,2)
      If STempArray(0,i) = STempId Then
         'output session start and end time
         Response.Write STempArray(1,i) & " - " & STempArray(2,i)
         'determine number of minutes differance
         temp_time = DateDiff("n",STempArray(1,i),STempArray(2,i))
         'output Session time?
         Response.Write " (" & FmtTime(temp_time) & ")<br>"
         'add session to total
         total_time = total_time + temp_time
      End If
   Next
   'output total time
   Response.Write "Total Time: " & FmtTime(total_time) & "<br>"
End Sub

Function FmtTime(numMins)
   'added a cheap zerofill on the minutes
   FmtTime = Fix(numMins/60) & ":" & Right("00" & (numMins mod 60),2)
End Function

Additional Suggestions:
- Checking for non-null dates might be a good thing to add, since a NULL will make this blow up
- Using FormatDateTime is unnecessary for using the dates for processing. I would actually suggest using it in your Response.Write funcitons to standardize their output and then add CDate's in the DateDiff
- I would also consider converting this to a function and returning the data as a delimited string, array, or custom object. Embedding your content output in the function is going to make it more difficult to edit later on as yu will have to track down the funciton to change the presentation of the data, whereas if you call the function and get back the output, you can control the presentation of that data back where you are outputting other HTML and text.

Just some thoguhts I had as I was looking this over,
-T


barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top