?
Solved

Check if string is in time formate (01:00:00)

Posted on 2007-08-02
7
Medium Priority
?
195 Views
Last Modified: 2010-04-15
I need to check if a string in in a time format of 01:00:00 to 24:00:00. In addition i need the check to throw a true or false as it is in a If statement.

Nugs
0
Comment
Question by:Nugs
  • 5
  • 2
7 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 19620056
A typical way to do this would be to use a Regular Expression.

I assume you mean RETURN true or false rather than THROW true or false.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 19620101
A very quick and dirty check on a string in a variable called "foo" would be
Regex.IsMatch(foo, "[0-2][0-9]:[0-5][0-9]:[0-5][0-9]", RegexOptions.None)
but this will allow some invalid values to slip by...
such as 29:00:00, for example.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 19620142
More CORRECT but not as easy to understand is
Regex.IsMatch(foo, "([0-1][0-9]|2[0-4]):[0-5][0-9]:[0-5][0-9]", RegexOptions.None)
which allows ALL hours beginning with 0 or 1, but requires a singels digit of 0-4 if the hour begins with "2".

You might refine this  using the "|" alternation if you want to disallow, say 24:00:01, or if you want to ALLOW 1:00:00 or 12:1:2 (not require leading zeroes).
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:Nugs
ID: 19620193
Yes i do mean Return... the ONLY values that i will need to check are as follows:

01:00:00
02:00:00
03:00:00
04:00:00
05:00:00
06:00:00
07:00:00
08:00:00
09:00:00
10:00:00
11:00:00
12:00:00
13:00:00
14:00:00
15:00:00
16:00:00
17:00:00
18:00:00
19:00:00
20:00:00
21:00:00
22:00:00
23:00:00
24:00:00

But if the value comes over as "thisisastringvalue", i need to catch it...

nugs
0
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 2000 total points
ID: 19620236
Regex.IsMatch(foo, "([0-1][0-9]|2[0-4]):00:00", RegexOptions.None)
   will return true if good, false if bad.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 19620246
(remember to do a
using System.Text.RegularExpressions;
at the top of your file, or you have to say

System.Text.RegularExpressions.Regex.IsMatch(foo, "([0-1][0-9]|2[0-4]):00:00", RegexOptions.None)

instead :-)
0
 
LVL 2

Author Comment

by:Nugs
ID: 19620373
Thanks you very much, YOU ROCK!

Nugs
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question