简体   繁体   中英

Calculate total time between Dates in Hours and Minutes

Hi

I'm trying to resolve a problem using VB and I need some assistance. I'm very new to the language (1 week). The problem is I have created a user form to show how many hours and minutes has elapsed between two different times similar to a time sheet.

The user form consists of two calendars, and under each calendar there are two text boxes; one box each to record the Hour and Minute they left and two further boxes to record the time they arrived back.

I have used the code to minus the calendars together (eg calendar in – calendar out) then times this by 24 to indicate the hours away. Then under the calendar out I have a text box for the user to type in the hour they left. Then I minus the 24 by the Hour out eg if it was 24 -15 it will appear 9 ( 9 hours of that day ) then I would add that to the figure they inserted in the text box Hour in (Return Time). eg 14. Then I would add them to together eg 9 + 14 = 23 and have this displayed in another text box Total Hours. Therefore it would display 23 meaning 23 hours.

I have then want to show another two text boxes to indicate minutes. One for Minutes Out then Minutes In.

I have the problem to convert these minutes for instance if it is the out time is 15:50 and the in time the next day is at 15:55 it displays as 24 (in one text box) and 105 minutes (in the other text box). I would like the minutes added to the hour and have the balance of the remaining minutes in the minute text box. This should display 24 (in one text box) and 5 (in another text box).

The ultimate aim is to get a result that shows a person was absent for a number of days, hours and minutes, eg, 2 days, 5 hours and 10 minutes. Any ideas on how I can modify my code to achieve this?

Here's my code.

Please Help

Dim number1 As Date
Dim number2 As Date
Dim number3 As Integer
Dim number4 As Integer
Dim Number5 As Integer
Dim Number6 As Integer
Dim answer  As Integer
Dim answer2 As Integer
Dim answer3 As Integer
Dim answer4 As Integer
Dim answer5 As String

number1 = DTPicker1
number2 = DTPicker2
number3 = Txthourout
number4 = TxtHourin
Number5 = TxtMinuteout
Number6 = TxtMinuetIn


answer = number2 - number1
answer2 = answer * 24
answer3 = answer2 - number3
answer4 = answer3 + number4

answer5 = Number5 + Number6


TextBox1.Text = answer4
TextBox2.Text = answer5

End Sub

Ok for the answer you should be informed that Date contains the date and time and it is actually stored as a numerical value where the date makes up for the integer part (although it can be larger then the nax value for Integer so make sure you use variable type Long to save the date part).

Next If you take the difference between 2 dates and put that answer in a Date variable again it will easily give you the answers you are looking for:

dtDiff = dtEnd - dtStart

As you can now perform the following on this:

intDaysDiff = Int(dtDiff) 'As the number of days between the two dates is expected to be small enough we can use the Int() function
intHoursDiff = Hour(dtDiff)
intMinutesDiff = Minute(dtDiff)

For retreiving minutein hourin and such just use the same method

report them back as you want for the user in the user form.

This should get you started. See this link for the date diff syntax.

Dim d1 As Date, d2 As Date, diff As Variant
'sample dates
d1 = DateTime.Now   '<--you'd use your calendar 1 here
d2 = DateTime.DateAdd("h", 4, d1)   '<--you'd use your calendar 2 here

'change "h" to what ever part of the different you want.
diff = DateTime.DateDiff("h", d1, d2)

Also take a look at the CDate and Format functions. They can be used to ensure something is in the proper date format and convert dates to your preferred string representations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM