Results 1 to 4 of 4

Thread: time defference anyone?

  1. #1

    Thread Starter
    New Member nxx7's Avatar
    Join Date
    Dec 2004
    Location
    Kuala Lumpur, Malaysia
    Posts
    6

    time defference anyone?

    helohhh...

    i want to know how to calculate diffrence between two time...

    the situation is somewhat like this...

    once you've enter the system... you'll be clocked in automatically... after an hour or so, you've decided to exit from the system

    what can i do to get the difference between the clocked in time, and the clocked out time???

    anyone who can help show me the code? i really appreciate it... thanks a lot
    ---------------------------------------------------------------
    never afraid to ask, never hesitate to learn, never stop trying...

  2. #2

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: time defference anyone?

    It is my understanding that the timer will not trigger more than 18 times a second.

    The recommendation from MSDN is to check the system clock time for "time elapsed".

    To test this setup a timer to trigger very fast and use a global counter to track how many times it does "really" trigger.

    Use the system clock to run this for a set amount of time - lets say 10 seconds.

    If in that 10 seconds the counter only goes up to 180 (18 * 10) then you know that the timer doesn't trigger more than the clock interval.

  4. #4
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526

    Re: time defference anyone?

    Here's some old code I had that will do what you want without a timer:
    VB Code:
    1. Option Explicit
    2. Dim showTime As Boolean
    3. Dim startHour As String
    4. Dim startMinute As String
    5. Dim startSecond As String
    6. Dim startTotal As Long
    7. Dim currentHour As String
    8. Dim currentMinute As String
    9. Dim currentSecond As String
    10. Dim currentTotal As Long
    11. Dim elapsedHour As Long
    12. Dim elapsedMinute As Long
    13. Dim elapsedSecond As Long
    14. Dim elapsedTotal As Long
    15. Dim startAmPm As String
    16. Dim currentAmPm As String
    17. Dim ampmFlag As Boolean
    18.  
    19. Private Sub cmdUpdateRunTime_Click()
    20. Dim length As Long
    21.    
    22.     currentAmPm = Mid(Time, Len(Time) - 1, 2)
    23.  
    24.     'Set flag
    25.     If (startAmPm = "AM" And currentAmPm = "AM") Or (startAmPm = "PM" And currentAmPm = "PM") Then
    26.         ampmFlag = False
    27.     Else
    28.         ampmFlag = True
    29.     End If
    30.  
    31.     length = HowLong(startHour, startMinute, startSecond)
    32.     lblElapsedHours.Caption = elapsedHour
    33.     lblElapsedMinutes.Caption = elapsedMinute
    34.     lblElapsedSeconds.Caption = elapsedSecond
    35. End Sub
    36.  
    37. Private Sub Form_Load()
    38.  
    39.     lblStartTime.Caption = Time
    40.     startSecond = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 4, 2)
    41.     startMinute = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 7, 2)
    42.     If Len(lblStartTime.Caption) > 10 Then
    43.         startHour = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 10, 2)
    44.     Else
    45.         startHour = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 9, 1)
    46.     End If
    47.    
    48.     'Because of the way elapsed time is calculated, must keep
    49.     'track of AM/PM portion of Start time for comparison when
    50.     'elapsed time is eventually calculated
    51.     startAmPm = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 1, 2)
    52.     Me.Show
    53.     DisplayTime
    54.    
    55. End Sub
    56.  
    57. Public Function HowLong(ByVal startHour As String, ByVal startMinute As String, ByVal startSecond As String) As Long
    58.  
    59. currentSecond = Mid$(Time, Len(Time) - 4, 2)
    60. currentMinute = Mid$(Time, Len(Time) - 7, 2)
    61. If Len(Time) > 10 Then
    62.     currentHour = Mid$(Time, Len(Time) - 10, 2)
    63. Else
    64.     currentHour = Mid$(Time, Len(Time) - 9, 1)
    65. End If
    66.  
    67. If ampmFlag = True And currentHour <> 12 Then
    68.     currentHour = currentHour + 12
    69. End If
    70.    
    71. startTotal = (startHour * 3600) + (startMinute * 60) + startSecond
    72. currentTotal = (currentHour * 3600) + (currentMinute * 60) + currentSecond
    73. elapsedTotal = currentTotal - startTotal
    74.  
    75. 'Application is active less than 1 minute
    76. If elapsedTotal < 60 Then
    77.     elapsedHour = 0
    78.     elapsedMinute = 0
    79.     elapsedSecond = elapsedTotal
    80. End If
    81.  
    82. 'Application is active for more than 1 minute
    83. 'but less than 1 hour
    84. If elapsedTotal >= 60 And elapsedTotal < 3600 Then
    85.     elapsedHour = 0
    86.     elapsedSecond = elapsedTotal Mod 60
    87.     elapsedMinute = (elapsedTotal - elapsedSecond) / 60
    88. End If
    89.  
    90. 'Application is active for 1 hour or more
    91. If elapsedTotal >= 3600 Then
    92.     elapsedHour = (elapsedTotal - (elapsedTotal Mod 3600)) / 3600
    93.     elapsedSecond = (elapsedTotal - (elapsedHour * 3600)) Mod 60
    94.     elapsedMinute = (elapsedTotal - (elapsedHour * 3600) - elapsedSecond) / 60
    95. End If
    96.  
    97. End Function
    98.  
    99. Private Sub DisplayTime()
    100. Do While showTime = False
    101.     lblCurrentTime = Time
    102.     DoEvents
    103. Loop
    104.    
    105. End Sub
    106.  
    107. Private Sub Form_Unload(Cancel As Integer)
    108.     showTime = True
    109. End Sub
    Do canibals not eat clowns because they taste funny?

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