by Timothy Wolfe (1 Submission)
Category: Math/Dates
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sat 8th November 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Adding Hours from a schedule. ie.) if you worked from 9:06am to 4:30pm you would use this function to find out how many hours you worked.
API Declarations
Dim total As Double
rem these are used by the command button
Dim intime As String
Dim outtime As String
Dim montot As Double
rem these are used by houradder()
Rem Dim mon As String
Rem Dim mono As String
Dim outhour As Integer
Dim inhour As Integer
Dim inmin As Double
Dim outmin As Double
Dim timein As String
Dim timeout As String
Dim temp As String
Dim temptotal As Double
Rem now I'm going to call the houradder function 1 time for an example
Rem and then I'm going to add the totals together as long as the intime is not x
Rem then it will run houradder()
intime = Text1.Text
outtime = Text2.Text
If intime <> "x" Then
temp = houradder(intime, outtime)
montot = total
End If
Text3.Text = montot
tmp = Text3.Text
Text3.Text = Mid(tmp, 1, 5)
End Sub
Function houradder(mon As String, mono As String)
Rem Bring in user input
Rem these are remarked out because I put this code into a function instead of being
Rem code executed by a command button
Rem This is the code for finding out am and pm
temp = Right(mon, 4)
If Right(mon, 2) = "pm" Then
timein = "pm"
Else
timein = "am"
End If
If Right(mono, 2) = "pm" Then
timeout = "pm"
Else
timeout = "am"
End If
Rem Find out whether the times for in & out are 4 or 5 digits so we can
Rem break up minutes and hours
temp = Mid(mon, 2, 1)
If temp = ":" Then
inmin = Mid(mon, 3, 2)
inhour = Mid(mon, 1, 1)
Else
inmin = Mid(mon, 4, 2)
inhour = Mid(mon, 1, 2)
End If
temp = Mid(mono, 2, 1)
If temp = ":" Then
outmin = Mid(mono, 3, 2)
outhour = Mid(mono, 1, 1)
Else
outmin = Mid(mono, 4, 2)
outhour = Mid(mono, 1, 2)
End If
Rem now I need to convert PM clocks to 24 hour time and I need to make calculations
Rem relative to a standard hour?
If timeout = "pm" Then
outhour = outhour + 12
End If
If timein = "pm" Then
inhour = inhour + 12
End If
Rem Now I need to convert the time in minutes to minutes accrued.
inmin = 60 - inmin
Rem Now I'm ready to add the minutes & convert them to a number
temptotal = inmin + outmin
temptotal = temptotal / 60
Rem Now I need to take into account the differences in AM and PM when adding hours
Rem So I'll make a a different case for each combination that needs to be treated
Rem differently
Rem If you clock in and out in the AM
If timein = "am" Then
If timeout = "am" Then
total = outhour - inhour - 1
End If
End If
Rem If you clock in AM and out PM
If timein = "am" Then
If timeout = "pm" Then
If outhour = 24 Then
outhour = 12
End If
total = outhour - inhour - 1
End If
End If
Rem If you clock in PM and out PM
If timein = "pm" Then
If timeout = "pm" Then
If outhour = 24 Then
outhour = 12
End If
If inhour = 24 Then
inhour = 12
End If
If total = 1 Then
total = outhour - inhour
Else
total = outhour - inhour - 1
End If
End If
End If
Rem If you clock in PM and out AM
If timein = "pm" Then
If timeout = "am" Then
outhour = outhour + 12
inhour = inhour - 12
total = outhour - inhour - 1
End If
End If
Rem if the result is negative & both punch in and
Rem out are the same then add 24 to result
If total < -1 Then
If timein = timeout Then
total = total + 24
End If
End If
Rem minutes = inmin + tuem + wedm + thum + frim + satm
Rem minutes = minutes / 60
Rem total = mon + tue + wed + thu + fri + sat + minutes
Rem total = Mid(total, 1, 4)
total = total + temptotal
End Function
No comments have been posted about Adding Hours from a schedule. ie.) if you worked from 9:06am to 4:30pm you would use this function . Why not be the first to post a comment about Adding Hours from a schedule. ie.) if you worked from 9:06am to 4:30pm you would use this function .