Solved

# Check if string is valid time

Posted on 2007-07-24
1,805 Views
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
Question by:AWestEng

LVL 10

Assisted Solution

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

LVL 1

Author Comment

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

LVL 27

Assisted Solution

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

LVL 34

Accepted Solution

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

LVL 85

Assisted Solution

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

### Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.