
Thanks: 0
Likes: 0
Dislikes: 0

Junior Member
How to convert time into minutes in QTP
Hi Folks,
Hope you guys are doing good.
I have a small issue here.
I am writing a small program to see the user's duration.
For eg :
User 1  Logged in at 08:30:00 AM  Logged Out at 08:40:25 AM
User 2  Logged in at 09:34:11 AM  Logged Out at 10:40:25 AM
Next after that what i am doing is assigning Logged in time to Var A.
A = Logged in time
B = Logged out Time.
C = BA
In Case of User 1 , I should get 00:10:25 but im getting some random numbers as "0.001235689"
Any idea guys how to do it.
TIA.
Jason

Moderator
Re: How to convert time into minutes in QTP
Look at DateDiff function online help and do the math.

Member
Re: How to convert time into minutes in QTP
As long as those are time objects I would assume you would be able to do a small calculation to get the time elapsed between punches... via:
<font class="small">Code:</font><hr /><pre>
Dim tempTime1, tempTime2, elapsedTime
Dim tempHour, tempMin, tempSec
'This chunk will return the time in minutes with seconds as the decimal
tempTime1 = Hour(timeA) * 60
tempTime1 = tempTime1 + Minute(timeA)
tempTime1 = tempTime1 + (Second(timeA)/60)
tempTime2 = Hour(timeB) * 60
tempTime2 = tempTime2 + Minute(timeB)
tempTime2 = tempTime2 + (Second(timeB)/60)
'get the difference between the times
elapsedTime = tempTime2  tempTime1
tempHour = elapsedTime / 60
if tempHour < 1 then
tempHour = 0
else
tempHour = CInt(tempHour)
end if
tempMin = CInt( elapsedTime  (tempHour * 60) )
'should subtract the integers before the decimal point
tempSec = (elapsedTime  tempHour  tempMin) * 100
</pre><hr />
If my logic is correct which I think it is that should give you the elapsed time in 4 variables total (elapsedTime, tempHour, tempMin, tempSec) and then you can format it however you would like... Hope this helps [img]/images/graemlins/smile.gif[/img]
PS: I'm pretty sure there is a more efficient way to parse the elapsed time into hr/min/sec, but I just whipped this together real quick for you :P

Moderator
Re: How to convert time into minutes in QTP
Here you go.........
StartTime = "08:30:00 AM"
EndTime = "08:40:25 AM"
'Msgbox Right("0"&Round(DateDiff("n",StartTime,EndTime )/60,0),2) ' Get Hours
'Msgbox Right("0"&DateDiff("n",StartTime,EndTime) Mod 60,2) ' Get minutes
'Msgbox Right("0"&DateDiff("s",StartTime,EndTime) Mod 60,2) ' Get seconds
Msgbox Right("0"&Round(DateDiff("n",StartTime,EndTime )/60,0),2)&":"&Right("0"&DateDiff("n",St artTime,EndTime) Mod 60,2)&":"&Right("0"&DateDiff("s",Start Time,EndTime) Mod 60,2)
StartTime = "09:34:11 AM"
EndTime = "10:40:25 AM"
Msgbox Right("0"&Round(DateDiff("n",StartTime,EndTime )/60,0),2)&":"&Right("0"&DateDiff("n",St artTime,EndTime) Mod 60,2)&":"&Right("0"&DateDiff("s",Start Time,EndTime) Mod 60,2)

Member
Re: How to convert time into minutes in QTP
StartTime = "11:34:50 AM"
EndTime = "11:35:48 AM"
Msgbox Right("0"&Round(DateDiff("n",StartTime,EndTime )/60,0),2)&":"&Right("0"&DateDiff("n",St artTime,EndTime) Mod 60,2)&":"&Right("0"&DateDiff("s",Start Time,EndTime) Mod 60,2)
When we run this script we get the answer as 00:01:58
where as actually it is only 00:00:58. How can I change the above code to run correct all the time.

Senior Member
Re: How to convert time into minutes in QTP
Try The below code:
StartTime = "11:34:50 AM"
EndTime = "11:38:48 AM"
strseconds = DateDiff("s",StartTime,EndTime) 'Total seconds
strhours = strseconds \ 3600
If Len(strhours) = 1 Then strhours ="0" & strhours
strminutes = (strseconds \ 60) mod 60
If Len(strminutes) = 1 Then strminutes ="0" & strminutes
seconds = strseconds mod 60
If Len(seconds) = 1 Then seconds ="0" & seconds
MSgbox strhours & ":" & strminutes & ":" & seconds

Senior Member
Re: How to convert time into minutes in QTP
Use this...
Dot net factory will do all the formating and calculation for us [img]/images/graemlins/smile.gif[/img]
Its working fine for me...let me know if you face any issues...
Set dt1 = DotNetFactory.CreateInstance("System.DateTime")
Set dt2 = DotNetFactory.CreateInstance("System.DateTime")
Set dtmTimeSpan = DotNetFactory.CreateInstance("System.TimeSpan")
strStartTime = "4/24/2009 11:11:08 AM"
strEndTime = "4/27/2009 11:12:06 PM"
Set dtmStartTime = DotNetFactory.CreateInstance("System.Convert").ToD ateTime(strStartTime)
Set dtmEndTime = DotNetFactory.CreateInstance("System.Convert").ToD ateTime(strEndTime)
Set dtmTimeSpan = dtmEndTime.Subtract(dtmStartTime)
msgbox dtmTimeSpan.ToString()

Member
Re: How to convert time into minutes in QTP
Kalesh/QAAutoTester
strStartTime = "11:11:38 AM"
strEndTime = "12:05:06 AM"
The time shown above still does not work with any of the solution.

Senior Member
Re: How to convert time into minutes in QTP
if you want to compare time alone, we need to typecast the datetime to time.
I used date also so that even if our code is running overnight , we will get the correct response.
Dont have access to visual studio and QTP now. I can get you the details by my evening.
I would prefer using the date part also with the time.

Senior Member
Re: How to convert time into minutes in QTP
[ QUOTE ]
strStartTime = "11:11:38 AM"
strEndTime = "12:05:06 AM"
[/ QUOTE ]
Is the end time a valid time? In 12 hr format, it should be 00:05:06 AM, which would be the next day
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules
