473,322 Members | 1,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Validate a Time String

ABB
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

Nov 21 '05 #1
6 9534
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

"ABB" <AB*@discussions.microsoft.com> wrote in message
news:3B**********************************@microsof t.com...
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
Nov 21 '05 #2
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
----------------------
"ABB" <AB*@discussions.microsoft.com> wrote in message
news:3B**********************************@microsof t.com...
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
Nov 21 '05 #3
"Siva M" <sh******@online.excite.com> schrieb:
Private Function GetValidTime(ByVal atimestring As String) As String
Try
DateTime dt = DateTime.ParseExact (atimestring, "HH:mm", null);


'null' => 'Nothing'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #4
Thanks Herfried!

Sorry about it. :-(

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Ok**************@tk2msftngp13.phx.gbl...
"Siva M" <sh******@online.excite.com> schrieb:
Private Function GetValidTime(ByVal atimestring As String) As String
Try
DateTime dt = DateTime.ParseExact (atimestring, "HH:mm", null);


'null' => 'Nothing'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #5
ABB
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

"Siva M" wrote:
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

"ABB" <AB*@discussions.microsoft.com> wrote in message
news:3B**********************************@microsof t.com...
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

Nov 21 '05 #6
For validating date values, specify the exact date format you are expecting
for ParseExact.

"ABB" <AB*@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
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

"Siva M" wrote:
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

"ABB" <AB*@discussions.microsoft.com> wrote in message
news:3B**********************************@microsof t.com...
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

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tony Nelson | last post by:
I'd like to have a fast way to validate large amounts of string data as being UTF-8. I don't see a fast way to do it in Python, though: unicode(s,'utf-8').encode('utf-8) seems to notice at...
1
by: aevans1108 | last post by:
Greetings All If this is the wrong place to post this question, please give me a push in the right direction. Thanks. I know there has to be a simpler way to do this, but this is as simple a...
0
by: SHC | last post by:
Hi all, I have a VC++ .NET 2003 - Windows XP Pro PC. I created a Win32 console application in my VC++ .NET 2003 and copied validateDOM.cpp, books.xml and books.xsd (see the attached files below)...
5
by: Jeff Evans | last post by:
I have a custom composite control which has a validator for a textbox. The validator and textbox are declared in the class and created in the CreateChildControls() method Here is the code for the...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
4
by: Mike Fellows | last post by:
running IIS on a single server, hosting pages on an intranet basis, one single user out of 50 is having an unable to validate data issue how do i fix this (the microsoft KB is a little...
6
by: Jonny | last post by:
Hi, I'm trying to validate my xml against a xsd but I can't get it to work. Originally, I wanted to validate an xml string but since I didn't get that to work I tried to validate an xml file...
6
by: Solje | last post by:
Im developing an ASP.NET application used for maintinance purpose and it may be idle for some ours. The application crash with the error shown below when the user click on some contol in the...
5
by: shapper | last post by:
Hello, What is the Regex expression to validate a date time format as follows: dd-mm-yyyy hh:mm:ss An example: 20-10-2008 10:32:45
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.