Results 1 to 2 of 2

Thread: Calculate Hours Worked (Military Time Used)

Threaded View

  1. #1

    Thread Starter
    Addicted Member spyk3's Avatar
    Join Date
    Apr 2009
    Location
    Dothan, AL
    Posts
    218

    Calculate Hours Worked (Military Time Used)

    So i was given the task of writing a new employee payroll proggy custom to my employers work. One issue was calculating, on the fly, the hours worked.
    For example, one employee clocks in then later logs time out, immidiatly on clicking hours box, they should see a sub total of those current hours worked for that shift. I had alot of trouble gathering information for this and finally just wrote my own code. Do to lack of knowledge this may not be the best sub function in the world, but it does EXACTLY what i needed it to do, and since there is so little out there for help, i figured i would provide it here for others.

    to use the sub is easy
    select a textbox or richtext to have focus then fill in the blanks. i'll put axample at end

    the Sub Code:
    1. Public Sub countHours(ByVal timeIn As String, ByVal timeOut As String, ByVal hours As TextBox, ByVal ooPs As Object)
    2.         Try
    3.             Dim firstTime As Integer = Convert.ToInt16(timeIn)
    4.             Dim secondTime As Integer = Convert.ToInt16(timeOut)
    5.             If secondTime > firstTime Then
    6.                 Dim hrsTimeIn As DateTime = DateTime.ParseExact(timeIn, "HHmm", CultureInfo.InvariantCulture)
    7.                 Dim hrsTimeOut As DateTime = DateTime.ParseExact(timeOut, "HHmm", CultureInfo.InvariantCulture)
    8.                 Dim totalTime As TimeSpan = hrsTimeOut.Subtract(hrsTimeIn)
    9.                 hours.Text = totalTime.ToString
    10.             ElseIf firstTime > secondTime Then
    11.                 Dim zeroHour As String = "0000"
    12.                 Dim tfourHour As String = "2359"
    13.                 Dim oneMin As TimeSpan
    14.                 oneMin = New TimeSpan(0, 1, 0)
    15.                 Dim midnight As DateTime = DateTime.ParseExact(zeroHour, "HHmm", CultureInfo.InvariantCulture)
    16.                 Dim twentyFour As DateTime = DateTime.ParseExact(tfourHour, "HHmm", CultureInfo.InvariantCulture)
    17.                 Dim hrsTimeIn As DateTime = DateTime.ParseExact(timeIn, "HHmm", CultureInfo.InvariantCulture)
    18.                 Dim hrsTimeOut As DateTime = DateTime.ParseExact(timeOut, "HHmm", CultureInfo.InvariantCulture)
    19.                 Dim firstDay As TimeSpan = twentyFour.Subtract(hrsTimeIn).Add(oneMin)
    20.                 Dim secondDay As TimeSpan = hrsTimeOut.Subtract(midnight)
    21.                 Dim totalTime As TimeSpan = firstDay.Add(secondDay)
    22.                 hours.Text = totalTime.ToString
    23.             End If
    24.         Catch ex As Exception
    25.             ooPs.focus()
    26.             MsgBox("Incorrect Time Value Entered, " & vbCrLf & "Please Use Military Time. " & vbCrLf & "exp. 1:35PM = 1335")
    27.         End Try
    28.     End Sub

    use like this

    the Use Code:
    1. Private Sub txtTransHours_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTransHours.GotFocus
    2.         countHours(txtTransTimeIn.Text, txtTransTimeOut.Text, txtTransHours, txtTransTimeIn)
    3.     End Sub

    it works! and it's simple! if anyone has better way, please post it!
    PS. this code also works for night shifts! like 11:00P to 7:00A and stuff!

    Night shift example


    Night shift example 2
    Last edited by spyk3; Oct 29th, 2009 at 02:50 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width