'****************************************************
'*
'* Simple application employing the use of a Timer to
'* determine what time an application started, keep track
'* of the current time, and use those two pieces of info
'* to determine how long the application has been running.
'*
'* Primary purpose for developing this application was to
'* have a tool to keep track of how long I've been coding
'* an application for billing purposes.
'*
'****************************************************
Option Explicit
Dim startHour As String
Dim startMinute As String
Dim startSecond As String
Dim startTotal As Long
Dim currentHour As String
Dim currentMinute As String
Dim currentSecond As String
Dim currentTotal As Long
Dim elapsedHour As Long
Dim elapsedMinute As Long
Dim elapsedSecond As Long
Dim elapsedTotal As Long
Dim startAmPm As String
Dim currentAmPm As String
Dim ampmFlag As Boolean
Private Sub cmdUpdateRunTime_Click()
Dim length As Long
currentAmPm = Mid(Time, Len(Time) - 1, 2)
'Set flag
If (startAmPm = "AM" And currentAmPm = "AM") Or (startAmPm = "PM" And currentAmPm = "PM") Then
ampmFlag = False
Else
ampmFlag = True
End If
length = HowLong(startHour, startMinute, startSecond)
lblElapsedHours.Caption = elapsedHour
lblElapsedMinutes.Caption = elapsedMinute
lblElapsedSeconds.Caption = elapsedSecond
End Sub
Private Sub Form_Load()
Dim a As String
Timer1.Interval = 1
lblStartTime.Caption = Time
startSecond = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 4, 2)
startMinute = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 7, 2)
If Len(lblStartTime.Caption) > 10 Then
startHour = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 10, 2)
Else
startHour = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 9, 1)
End If
'Because of the way elapsed time is calculated, must keep
'track of AM/PM portion of Start time for comparison when
'elapsed time is eventually calculated
startAmPm = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 1, 2)
End Sub
Private Sub Timer1_Timer()
lblCurrentTime.Caption = Time
End Sub
Public Function HowLong(ByVal startHour As String, ByVal startMinute As String, ByVal startSecond As String) As Long
currentSecond = Mid$(Time, Len(Time) - 4, 2)
currentMinute = Mid$(Time, Len(Time) - 7, 2)
If Len(Time) > 10 Then
currentHour = Mid$(Time, Len(Time) - 10, 2)
Else
currentHour = Mid$(Time, Len(Time) - 9, 1)
End If
If ampmFlag = True And currentHour <> 12 Then
currentHour = currentHour + 12
End If
startTotal = (startHour * 3600) + (startMinute * 60) + startSecond
currentTotal = (currentHour * 3600) + (currentMinute * 60) + currentSecond
elapsedTotal = currentTotal - startTotal
'Application is active less than 1 minute
If elapsedTotal < 60 Then
elapsedHour = 0
elapsedMinute = 0
elapsedSecond = elapsedTotal
End If
'Application is active for more than 1 minute
'but less than 1 hour
If elapsedTotal >= 60 And elapsedTotal < 3600 Then
elapsedHour = 0
elapsedSecond = elapsedTotal Mod 60
elapsedMinute = (elapsedTotal - elapsedSecond) / 60
End If
'Application is active for 1 hour or more
If elapsedTotal >= 3600 Then
elapsedHour = (elapsedTotal - (elapsedTotal Mod 3600)) / 3600
elapsedSecond = (elapsedTotal - (elapsedHour * 3600)) Mod 60
elapsedMinute = (elapsedTotal - (elapsedHour * 3600) - elapsedSecond) / 60
End If
End Function