Calculating Military Time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Serenityquinn15
    New Member
    • Oct 2007
    • 30

    Calculating Military Time

    Hi!
    I used Query in Access to calculate the time in and out of the employee of course to get the total minutes, however i've been encountered a problem. when i put for timeIn=22:00 and for time Out =24:00 the total mins display like this:" -1320 ", which actually wrong. it should be "120" mins instead of "-1320". how i can fix this problem??? i already set those data into short time format.
    Please help me.... thank you....
  • salimudheen
    New Member
    • Jan 2007
    • 14

    #2
    Hey,
    Try this simple method,

    Option Compare Database
    Dim minsIn As Long
    Dim minsOut As Long
    Private Sub TimeIn_Exit(Can cel As Integer)
    If TimeIn = 0# Then
    minsIn = 1440
    Else
    minsIn = VBA.DatePart("h ", TimeIn) * 60
    minsIn = minsIn + VBA.DatePart("n ", TimeIn)
    End If
    End Sub
    Private Sub TimeOut_Exit(Ca ncel As Integer)
    If TimeOut = 0# Then
    minsOut = 1440
    Else
    minsOut = VBA.DatePart("h ", TimeOut) * 60
    minsOut = minsOut + VBA.DatePart("n ", TimeOut)

    End If
    End Sub

    Private Sub TotalTime_Enter ()
    TotalTime = minsOut - minsIn
    End Sub

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      1. Midnight should be expressed as 00:00 and not 24:00.
      2. You must allow for the post-Midnight scenario by adding a Day in Minutes (1,440).
      3. Based on a [StartTime] and [EndTime] Fields, and a Table named tblTest, the following Query using a simple Public Function should solve the problem:

      Code:
      SELECT tblTest.StartTime, tblTest.EndTime, fCalcDiffInMinutes([StartTime],[EndTime]) AS [Interval]
      FROM tblTest;
      Code:
      Public Function fCalcDiffInMinutes(dteStart As Date, dteFinish As Date) As Long
      Dim intDifference As Integer
      
      intDifference = DateDiff("n", dteStart, dteFinish)
      
      If intDifference < 0 Then
        fCalcDiffInMinutes = intDifference + 1440
      Else
        fCalcDiffInMinutes = intDifference
      End If
      End Function
      Test Data (tblTest):
      Code:
      StartTime	EndTime
      12:00	     13:00
      22:00	     23:50
       6:00	      8:00
      22:00	      0:00
      23:00	      4:00
      12:00	     13:30
      OUTPUT:
      Code:
      StartTime	EndTime	Interval
      12:00	     13:00	       60
      22:00	     23:50	      110
       6:00	      8:00	      120
      22:00	      0:00	      120
      23:00	      4:00	      300
      12:00	     13:30	       90

      Comment

      • Serenityquinn15
        New Member
        • Oct 2007
        • 30

        #4
        Hi!
        It works good!!! Thanks for the help!

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Originally posted by Serenityquinn15
          Hi!
          It works good!!! Thanks for the help!
          You are quite welcome.

          Comment

          Working...