Solved

How to calculate total hours and minutes between two time? (Urgent)

Posted on 2002-03-28
8
201 Views
Last Modified: 2010-05-02
In my form, it has two text box that i set its format to ##:## using MaskEdit.

These two textboxes are for user to key in time.
From 01:00  To 11:15

When user click the calculate button, program will calculate the time.  This is use to calculate total working hours.

The result.. should be 10:15, right?  how to do this?  any VB function can calculate this directly?

I used DateDiff function to calculate and return in minutes but the result not accurate. If not mistaken, it return 1 hours for this.

I have to save the calculation result in 10.15 (numberic field) so that i can use it to calculate total salary.  10.15 means 10 hours + 10 minutes.

I know the design is not good.. any good suggestion?
I have to save the total hours in numberic data type because in other forms, i used SQL to calculate total salary.  if save in string, the result will not accurate.

Please tell me how to solve this problem, thanks.
0
Comment
Question by:yongyih
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 6901964
Hi yongyih,

you can use datediff

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

HTH:O)Bruintje
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6901966
Hi yongyih,

Try this:

Private Sub Command3_Click()
    Text1 = "13:00"
    Text2 = "23:15"
    Dim starttime As Date
    Dim stoptime As Date
    starttime = Date + CDate(Text1)
    stoptime = Date + CDate(Text2)
    MsgBox Format(stoptime - starttime, "hh:mm")
End Sub
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6901984
you used it already, i'm getting lazy

you have to format the result

Sub test()

Dim TimeOne As String
Dim TimeTwo As String

TimeOne = TimeValue(Now)
TimeTwo = TimeValue(NOw-.5)

MsgBox Format$(TimeValue(dteOut) - TimeValue(dteIn), "h:mm:ss")

end sub

HTH:O)Bruintje
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 44

Accepted Solution

by:
bruintje earned 20 total points
ID: 6901985
oops....

Sub test()

Dim TimeOne As String
Dim TimeTwo As String

TimeOne = TimeValue(Now)
TimeTwo = TimeValue(Now - 0.5)

MsgBox Format$(TimeValue(TimeOne) - TimeValue(TimeTwo), "h:mm:ss")

End Sub
0
 
LVL 2

Author Comment

by:yongyih
ID: 6902278
I try first, thanks.
0
 
LVL 4

Expert Comment

by:RichW
ID: 6902329
Try this:

Public Function TimeDiff(ByVal Interval As String, ByVal Time1 As String, ByVal Time2 As String)
   
    Dim HourTime1, HourTime2, MinuteTime1, MinuteTime2, SecondTime1, SecondTime2, TimeDifference
    Dim Time1InSeconds, Time2InSeconds
    Time1 = FormatDateTime(Time1, vbLongTime)
    Time2 = FormatDateTime(Time2, vbLongTime)
    HourTime1 = Hour(Time1)
    HourTime2 = Hour(Time2)
    MinuteTime1 = Minute(Time1)
    MinuteTime2 = Minute(Time2)
    SecondTime1 = Second(Time1)
    SecondTime2 = Second(Time2)
    Time1InSeconds = SecondTime1 + (MinuteTime1 * 60) + (HourTime1 * 3600)
    Time2InSeconds = SecondTime2 + (MinuteTime2 * 60) + (HourTime2 * 3600)
    TimeDifference = Time2InSeconds - Time1InSeconds
    Select Case Interval
        Case "h"
            TimeDifference = TimeDifference / 3600
        Case "m"
            TimeDifference = TimeDifference / 60
    End Select
    TimeDiff = TimeDifference
End Function

Call it this way,

TIME = TimeDiff("h", timStart, timNow)

Use "h" "m" "s" for hours minutes and seconds.

Good luck!
RichW
0
 
LVL 2

Author Comment

by:yongyih
ID: 6904704
Thanks everyone for your reply. ^_^
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6904738
u're welcome thanks for the points
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question