[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1844
  • Last Modified:

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?
0
AWestEng
Asked:
AWestEng
4 Solutions
 
adriankohwsCommented:
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
 
planoczCommented:
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
 
SanclerCommented:
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
 
Mike TomlinsonMiddle 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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now