Validate a Time String

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Private Function GetValidTime(ByVal atimestring As String) As String

I am writing a function that accepts a string that may contains a time in
format "HH:MM" or it may contain junk data.

How can I check whether the string passed in this function is a valid time.
The function should return a valid time string otherwise it returns "00:00"
in case if invalid timestring was passed.

How can I write that function?


ABB
 
This is one way to do it:

Private Function GetValidTime(ByVal atimestring As String) As String
Try
DateTime dt = DateTime.ParseExact (atimestring, "HH:mm", null);
Return atimestring
Catch
Return "00:00"
End Try
End Function

Private Function GetValidTime(ByVal atimestring As String) As String

I am writing a function that accepts a string that may contains a time in
format "HH:MM" or it may contain junk data.

How can I check whether the string passed in this function is a valid time.
The function should return a valid time string otherwise it returns "00:00"
in case if invalid timestring was passed.

How can I write that function?


ABB
 
Hi,

I would use a regular expression to validate the time. The
rgular expressions ismatch method will tell you if it is a valid format.

Dim regTime As New
System.Text.RegularExpressions.Regex("^([0-1][0-9]|[2][0-3]):([0-5][0-9])$")
Dim t1 As String = "25:12"
Dim t2 As String = "12:45"
Dim t3 As String = "01:12"

Trace.WriteLine(regTime.IsMatch(t1))
Trace.WriteLine(regTime.IsMatch(t2))
Trace.WriteLine(regTime.IsMatch(t3))


Ken
----------------------
Private Function GetValidTime(ByVal atimestring As String) As String

I am writing a function that accepts a string that may contains a time in
format "HH:MM" or it may contain junk data.

How can I check whether the string passed in this function is a valid time.
The function should return a valid time string otherwise it returns "00:00"
in case if invalid timestring was passed.

How can I write that function?


ABB
 
Siva M said:
Private Function GetValidTime(ByVal atimestring As String) As String
Try
DateTime dt = DateTime.ParseExact (atimestring, "HH:mm", null);

'null' => 'Nothing'.
 
Thanks Herfried!

Sorry about it. :-(

Siva M said:
Private Function GetValidTime(ByVal atimestring As String) As String
Try
DateTime dt = DateTime.ParseExact (atimestring, "HH:mm", null);

'null' => 'Nothing'.
 
Hi

I tested the solution by replacing 'null' with 'nothing'.

Try passing atimestring as " :" and u will get an exception.

how will this routine work for any invalid string date in atimestring?

ABB
 
For validating date values, specify the exact date format you are expecting
for ParseExact.

Hi

I tested the solution by replacing 'null' with 'nothing'.

Try passing atimestring as " :" and u will get an exception.

how will this routine work for any invalid string date in atimestring?

ABB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top