Time algorithm


‘I have two combo boxes, begTimeCmb and endTimeCmb
‘whenever they lose focus they call this algorithm to ensure that time is ‘entered correctly
‘pass a 1 in if you’re dealing with begTimeCmb and a 2 for endTimeCmb

‘my program passes in a 1 for the beginning time
‘and a 2 for the ending time

Private Sub timeAlgorithm(inte As Integer)
Dim origTime As String
Dim alterTime As String
Dim finalTime As String
Dim evening As Boolean
evening = False
Dim tempo As String
Dim beg As Boolean
Dim colonLoc As Integer
Dim colon As Boolean
colon = False
Dim hour As String
Dim minute As String

If inte = 1 Then
beg = True
origTime = begTimeCmb.Text
Else
beg = False
origTime = endTimeCmb.Text
End If

origTime = LCase(Trim(origTime))

For I = 1 To Len(origTime) Step 1
If Mid(origTime, I, 1) = “p” Then
evening = True
End If
Next

While Left(origTime, 1) = “0” Or Left(origTime, 1) = “:”
origTime = Mid(origTime, 2, Len(origTime))
Wend

For I = 1 To Len(origTime) Step 1
temp = Mid(origTime, I, 1)
If temp = “0” Or temp = “1” Or temp = “2” Or temp = “3” Or temp = “4” Or temp = “5” _
Or temp = “6” Or temp = “7” Or temp = “8” Or temp = “9” Or temp = “:” Then
If temp = “:” Then
If colon = False Then
alterTime = alterTime & temp
colonLoc = Len(alterTime)
End If
Else
alterTime = alterTime & temp
End If

If temp = “:” Then
colon = True
End If

End If
Next

If colon = True Then
If Len(alterTime) – colonLoc >= 2 Then
minute = Mid(alterTime, colonLoc + 1, 2)
hour = Left(alterTime, colonLoc – 1)
End If

If Len(alterTime) – colonLoc = 1 Then
hour = Left(alterTime, colonLoc – 1)
minute = Mid(alterTime, colonLoc + 1, 1) & “0”
End If

If Len(alterTime) – colonLoc = 0 Then
hour = alterTime
minute = “00”
End If

Else
If Len(alterTime) = 3 Then
minute = Mid(alterTime, 2, 2)
hour = Left(alterTime, 1)
End If

If Len(alterTime) >= 4 Then
minute = Mid(alterTime, 3, 2)
hour = Left(alterTime, 2)
End If

If Len(alterTime) = 2 Then
minute = “00”
hour = alterTime
End If

If Len(alterTime) = 1 Then
minute = “00”
hour = alterTime
End If

If Len(alterTime) = 0 Then
hour = “12”
minute = “00”
End If
End If

While hour > 12
hour = hour – 12
evening = True
Wend

If minute > 59 Then
minute = 59
End If

If evening = True Then
finalTime = hour & “:” & minute & “pm”
Else
finalTime = hour & “:” & minute & “am”
End If

If beg = True Then
begTimeCmb.Text = finalTime
Else
endTimeCmb.Text = finalTime
End If

End Sub

‘author:Dan O’Conor

’email:djo22@cornell.edu

Releated link:

Tutorial Visual Basic

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s