Check if string is valid time

Hi

How can I chak for a vaild time and only for a time

DIm strTIme as string = "16:34"

 If IsDate(strTIme ) And strTIme .Length = 5 Then
     'OK
else
    'Fault
End if

The probelm is that is also takes a date 05/05 => OK

How can I check that so it only approve the string if it's vaild time?
LVL 1
AWestEngAsked:
Who is Participating?
 
SanclerConnect With a Mentor Commented:
This will work on VB.NET 2005

        Dim t As DateTime
        MsgBox(DateTime.TryParseExact(TextBox1.Text, "HH:mm", Nothing, Nothing, t))

TryParseExact is not available in earlier versions, but you could use this

        Dim t As DateTime
        Try
            t = DateTime.ParseExact(TextBox1.Text, "HH:mm", Nothing)
            MsgBox("Valid")
        Catch ex As Exception
            MsgBox("Invalid")
        End Try

If you want times valid on the 12-hour, rather than the 24-hour, clock then substitute "hh:mm" for "HH:mm"

Roger
0
 
adriankohwsConnect With a Mentor Commented:
A bit stupid code but it works if you use the time this way.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click
        If CheckTime(TextBox1.Text) = True Then
            MsgBox("OK")
        Else
            MsgBox("Not OK")
        End If
    End Sub


    Private Function CheckTime(ByVal txt As String) As Boolean  'Must be format xx:xx
        Dim ch As String
        Dim i As Integer

        CheckTime = True
        For i = 1 To 2
            ' See if the next character is a non-digit.
            ch = Mid$(txt, i, 1)
            If ch < "0" Or ch > "9" Then
                ' This is not a digit.
                CheckTime = False
                Exit For
            End If
        Next i

        If IsNumeric(Mid$(txt, 1, 2)) Then
            If CInt(Mid$(txt, 1, 2)) > 24 Then
            End If
            CheckTime = False 'More than 24 hours
        End If

        If IsNumeric(Mid$(txt, 4, 2)) Then
            If CInt(Mid$(txt, 1, 2)) > 59 Then
            End If
            CheckTime = False 'More than 59 minutes
        End If


        For i = 4 To 5
            ' See if the next character is a non-digit.
            ch = Mid$(txt, i, 1)
            If ch < "0" Or ch > "9" Then
                ' This is not a digit.
                CheckTime = False
                Exit For
            End If
        Next i

        If Mid$(txt, 3, 1).ToString <> ":" Then
            CheckTime = False
        End If


    End Function
0
 
AWestEngAuthor Commented:
I found this.. but is's somethinh wrong with it

    Public Function IsTime(ByVal Time As Object) As Boolean
        '*****************************************************************
        '* This subroutine determines if a value is a valid time
        '* (not  date).
        '************************************************************

        If IsDate(Time) = True Then
            If CStr(Time) Like "*#*.*#*" = False Then
                If Fix(CDate(Time)) = 0 Then
                    If CStr(Time) Like "1[3-9]*[aApP]*" = False Then
                        If CStr(Time) Like "2[0-3]*[aApP]*" = False Then
                            IsTime = True
                        End If
                    End If
                End If
            End If
        End If

    End Function
0
 
planoczConnect With a Mentor Commented:
Hi AWestEng,
Here is a sample for you....

 Imports System.Text.RegularExpressions
 
Public Shared Function ValidHourTime(ByVal timeText As String) As Boolean
        Try
            Const OPT As String = "|"
            Dim patternTime12 As String = "^[0-1][0-9][:][0-5][0-9]$"
            Dim patternTime24 As String = "^[2][0-4][:][0-5][0-9]$"
            ' Combine all the patterns together with the optional
            ' separator '|'.
            Dim patternAll As String
            patternAll = patternTime12 & OPT
            patternAll += patternTime24

            Dim regParseTime As New System.Text.RegularExpressions.Regex(patternAll, RegexOptions.Multiline)
            Return regParseTime.IsMatch(timeText)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Function 'ValidateTime'
0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Use TimeSpan.Parse():
http://msdn2.microsoft.com/en-us/library/system.timespan.parse.aspx

Or TimeSpan.TryParse() for VB.Net 2005:
http://msdn2.microsoft.com/en-us/library/system.timespan.tryparse.aspx

VB.Net 2003 (or below):

        Dim strTime As String = "16:34"
        Dim ts As TimeSpan
        Dim valid As Boolean = True
        Try
            ts = TimeSpan.Parse(strTIme)
        Catch ex As Exception
            valid = False
        End Try
        If valid Then
            Debug.Print("Valid: " & ts.Hours & " hrs, " & ts.Minutes & " mins")
        Else
            Debug.Print("Not Valid: " & strTIme)
        End If

VB.Net 2005:

        Dim strTime As String = "16:34"
        Dim ts As TimeSpan
        If TimeSpan.TryParse(strTime, ts) Then
            Debug.Print("Valid: " & ts.Hours & " hrs, " & ts.Minutes & " mins")
        Else
            Debug.Print("Not Valid: " & strTime)
        End If
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.