473,883 Members | 2,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validate a Time String

ABB
Private Function GetValidTime(By Val 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 9661
This is one way to do it:

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

"ABB" <AB*@discussion s.microsoft.com > wrote in message
news:3B******** *************** ***********@mic rosoft.com...
Private Function GetValidTime(By Val 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.Reg ularExpressions .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.IsMatc h(t1))
Trace.WriteLine (regTime.IsMatc h(t2))
Trace.WriteLine (regTime.IsMatc h(t3))
Ken
----------------------
"ABB" <AB*@discussion s.microsoft.com > wrote in message
news:3B******** *************** ***********@mic rosoft.com...
Private Function GetValidTime(By Val 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******@onlin e.excite.com> schrieb:
Private Function GetValidTime(By Val atimestring As String) As String
Try
DateTime dt = DateTime.ParseE xact (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******** ******@tk2msftn gp13.phx.gbl...
"Siva M" <sh******@onlin e.excite.com> schrieb:
Private Function GetValidTime(By Val atimestring As String) As String
Try
DateTime dt = DateTime.ParseE xact (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(By Val atimestring As String) As String
Try
DateTime dt = DateTime.ParseE xact (atimestring, "HH:mm", null);
Return atimestring
Catch
Return "00:00"
End Try
End Function

"ABB" <AB*@discussion s.microsoft.com > wrote in message
news:3B******** *************** ***********@mic rosoft.com...
Private Function GetValidTime(By Val 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*@discussion s.microsoft.com > wrote in message
news:F8******** *************** ***********@mic rosoft.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(By Val atimestring As String) As String
Try
DateTime dt = DateTime.ParseE xact (atimestring, "HH:mm", null);
Return atimestring
Catch
Return "00:00"
End Try
End Function

"ABB" <AB*@discussion s.microsoft.com > wrote in message
news:3B******** *************** ***********@mic rosoft.com...
Private Function GetValidTime(By Val 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
6928
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 least some of the time (the unicode() part works but the encode() part bombs). I don't consider a RE based solution to be fast. GLib provides a routine to do this, and I am using GTK so it's
1
9112
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 way as I could come up with. Yes, it's obvious I don't know what I'm doing. I'm creating an XSD string and an XML string at runtime (ie: NOT read
0
4596
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) from Microfost MSDN Library for my project "validateXML". When I did "Build" on my project, I got the following fatal error C1010: c:\Documents and Settings\SHC\My Documents\Visual Studio Projects\valoidateDOM\valoidateDOM.cpp(273): fatal error...
5
2637
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 textbox, in CreateChildControls() searchBox = new TextBox(); searchBox.ID = "searchBox"; searchBox.EnableViewState = false; It is then added to a TableCell, which is added to a row, etc.
7
31853
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 current date. If not, I'd like to display the error message to ValidationSummary. It seems to make sense to me to use CompareValidator but the problem is put the current date into CompareValidator. So, I created a hidden text field in my aspx. ...
4
5773
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 inconclusive) ? cheers mark this is the error message
6
2759
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 instead. Didn't work either. Actuallty, the xml gets loaded but there are no events raised that says my xml is incorrect! I'm using .NET 2.0 and my code is listed below. ALL help is appreciated! Thanks!
6
3405
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 appliaction. This crash occurs on every clients at same time, event for the client at the local server. I added some logging to the Application_Error method that contains following: Exception of type System.Web.HttpUnhandledException was thrown. at...
5
13851
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
9797
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5807
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6005
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4622
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3241
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.