by Dan O'Connor ()
Category: String Manipulation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Mon 22nd May 2000
Date Added: Mon 8th February 2021
Rating:
(1 Votes)
Time algorithm. Accepts user input for a time and then not only checks to see if they enter the time correctly, but then figures out what they
API Declarations
'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
'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
No comments have been posted about Time algorithm. Accepts user input for a time and then not only checks to see if they enter the tim. Why not be the first to post a comment about Time algorithm. Accepts user input for a time and then not only checks to see if they enter the tim.