[Webinar] Streamline your web hosting managementRegister Today

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 381

# time calculation - VB6

I have a problem with calculating hours.  For example, when my DaysofActivity = 13 and LengthofActiviy = "12:00"  then I get a Result = "12:00"

I think it is calculating based on time of day.  How can I multiply 13 * 12:00 and get 156 hours.  What is wrong

``````Dim LengthofActivity As String
Dim DaysofActivity As String

Dim a As Date

DaysofActivity = rec2.Fields(3)
LengthofActivity = rec2.Fields(2)

a = CDate(LengthofActivity)
Result = DaysofActivity * a
``````
0
al4629740
• 4
• 3
• 2
• +2
4 Solutions

Commented:
Dim LengthofActivity As String
Dim DaysofActivity As String
Dim Result

Dim a As Date

DaysofActivity = 13
LengthofActivity = "12:00"

a = CDate(LengthofActivity)
Result = DaysofActivity * (Hour(a) + Minute(a) / 60)
0

Author Commented:
What did you declare Result as?
0

RetiredCommented:
You need to multiply by 24:
``````Dim LengthofActivity As String
Dim DaysofActivity As String
Dim Result As Single
Dim a As Date

DaysofActivity = 13 'rec2.Fields(3)
LengthofActivity = "12:00" 'rec2.Fields(2)

a = CDate(LengthofActivity)
Result = DaysofActivity * a * 24
MsgBox Result
``````
0

Older than dirtCommented:
Dim LengthofActivity As String
Dim DaysofActivity As String
Dim Result As Double
Dim hours As Integer
Dim minutes As Integer
Dim a As Double
Dim strParts() As String

DaysofActivity = 13 'rec2.Fields(3)
LengthofActivity = "12:00" 'rec2.Fields(2)

strParts = Split(LengthofActivity, ":")

Result = (strParts(0) + (strParts(1) / 60)) * DaysofActivity
0

Commented:
A Single would work, but I left it as a generic variable just because I was rushing...  Visual Basic is good at determining the type for you, so when it sees you multiplying Hour() and Minute()/60 it'll make it a Single anyway.

UPDATE:  I stand corrected - If you leave it as a generic variable, VB will convert it to a Double, not a Single, when it gets to the "Result=" line
0

Commented:
I think the original problem is caused by DaysOfActivity variable being declared a String data type.

``````Dim LengthofActivity As String
Dim DaysofActivity As Variant

DaysofActivity = rec2.Fields(3)   '=13
LengthofActivity = rec2.Fields(2)   '="12:00"
Debug.Print (DaysofActivity * CDate(LengthofActivity)) * 24
``````
This will produce the 156 value you seek in the Immediate window

In this example, the (DaysofActivity * CDate(LengthofActivity)) expression, with the given values, will result in a value of 6.5, which is a value in days.  Since you want to express this value in terms of hours, we must multiply by 24 (the number of hours in a day.

If the LengthOfActivity value were "12:15", then the result displayed in the Immediate window would be  159.25 hours
0

Author Commented:
I will be redistributing the points again.

0

Older than dirtCommented:
You're welcome and I'm glad I was able to help.

My profile contains links to some articles I've written that may interest you.
Marty - MVP 2009 to 2012
0

RetiredCommented:
The salient question is, 'do you now have a full grasp of how VB treats date and the other associated other data types?'.

We tend, in this zone, and particularly on this (date manipulation)  subject, to give an answer without a full explanation. This is an inevitable result of the competitive spirit that is designed into the forum as well as our natural vanities to provide the first and, hopefully, the best answer.
0

Author Commented:
I also distributed the points incorrectly, hence why I asked the moderator to open it up for me.
0

Older than dirtCommented:
No problem.
0

Author Commented:
Collaboratively, everyone helped me out.  Thanks
0

## Featured Post

• 4
• 3
• 2
• +2
Tackle projects and never again get stuck behind a technical roadblock.