views:

114

answers:

3

Hey all, i am trying to figure out how to calculate the wage for an employee when they clock out. This is the code i am currently using:

 Dim theStartTime As Date
 Dim theEndTime As Date
 Dim totalTime As String

 theStartTime = "16:11:06"
 theEndTime = "18:22:01"
 totalTime = Format(CDbl((theEndTime - theStartTime) * 24), "#0.0")

So workable hours would be: 2h 11m

Right now, with my calculation code above, i get 2.2. What would i need to add in order to get it to calculate the correct time of 2:11 instead of 2:20?

David

+7  A: 

Note that 2.2 hours is not 2:20, it's 2:12.

Change

Format(CDbl((theEndTime - theStartTime) * 24), "#0.0")

to

Format(theEndTime - theStartTime, "h:mm")

You're getting the right value, just rounding it off when you print. theEndTime - theStartTime is a time span equal to the difference between the two times. As you discovered, multiplying it by 24 will give you the number of hours different. However, you then have to divide by 24 again to use date/time formatting.

Check out all the ways to format dates and time in VB6.

Daniel Rasmussen
How would i add a wage to that output of 2:11? The wage is 10.50.
StealthRT
Just wait until you have to factor in overtime. That is a major mess, considering the rules differ by location and in some cases by day of the week.
JohnFx
`paycheck = (theEndTime - theStartTime) * 24 * wage`. I hope that this is a project, and not something you're really going to use in your business. Don't forget about things like daylight savings time, because I'm not sure how `(theEndTime - theStartTime)` will be handled.
Daniel Rasmussen
A: 

You should also try casting it to the Currency type which can represent all numeric values (within 4 digits to the left of decimal point and 15 digits to the right).

Format(CCur((theEndTime - theStartTime) * 24), "#0.00")
ChaosPandion
The problem here is not rounding, it's representation.
Daniel Rasmussen
@Daniel - That is what I said didn't I?
ChaosPandion
Sorry - I worded my comment wrong; the problem is formatting of the output. Casting to a `Currency` wouldn't solve the problem that 2.2 hours is not 2:20.
Daniel Rasmussen
@Daniel - For this specific case, but what if the output was `2.1` which cannot be represented appropriately as a double?
ChaosPandion
True, but the OP is looking for accuracy to the minute - I don't think `Currency` is worth the trouble, in my opinion.
Daniel Rasmussen
Daniel is correct. Like i stated above, the output currently is 2h 18m but i need 2h 11m as it really **should** be.
StealthRT
+1  A: 

First, I highly suggest going to the .NET framework (with it's easy-to-use TimeSpan class) if possible.

But, dealing in VB6 you should be able to use the DATEDIFF function (and it's been many years since I've touched VB6 so the specific syntax might be a bit off

Dim iHours As Integer, iMins As Integer
iMins = DateDiff("n", theStartTime, theEndTime)
iHours = iMins / 60
iMins = iMins Mod 60
Stephen Wrighton
Some of us don't have the luxury of upgrading all of our legacy software to .Net. (Though I too would recommend it if possible.)
Daniel Rasmussen
I just get 11 when using your code, Stehpen.
StealthRT
I would expect 11 in iMins and the 2 in the iHours.
Stephen Wrighton