Neil Thompson

BizTalk | .NET | SQL |

  Home  |   Contact  |   Syndication    |   Login
  15 Posts | 20 Stories | 56 Comments | 55 Trackbacks

News

Article Categories

Archives

Post Categories

Fav BizTalk Blogs

I recently tried to validate a time input on an ASP.NET form and I didn't manage to find a good regular expression on the net for the times I was trying to validate.  I call these “natural times” becaues it's the way that most north americans write time. (12 hr clock, with : and whitespace separators). This won't take UTC dates or 24 hr times that I know of but it works well (so far) with the .Tostring(”t”) short time representation of times.

((1+[012]+)|([123456789]{1}))(((:|\s)[0-5]+[0-9]+))?(\s)?((a|A|p|P)(m|M))

It seems longer than necessary (they always seem to), but it's pretty forgiving for the user typing the time.I won't take the time to explain all of the features of the expression but you can use the following links to help test it out.

  1. My favorite regular expression testing site.
  2. Regular expression reference site.

Regular expressions are one of those things that you run into often enough that you need to know them, but not often enough that you ever really get good at them.  So each time we struggle to get back in that mindset and then do some trial and error to get through the expression development.  I'm keeping this on my blog mostly for my own sake, so that I have it again when I need it. 

But feel free to lift it and give me feedback.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Thursday, July 20, 2006 7:46 PM

Feedback

# re: Regular expression for Time of day 6/28/2008 8:38 AM Manoj
Hi,
thanks for the article.

can u give some sample output what this expression takes correct vale.
I am not not able it figure out.






# re: Regular expression for Time of day 6/23/2009 12:45 PM Jeff Ancel
7:30PM, 7:30 PM, etc.

# re: Regular expression for Time of day 6/23/2009 12:48 PM Jeff Ancel
I forgot to leave this comment. Here is one (thanks for what you have done), that I modified to have the m optional and to also accept the HH:MM A/P or AM/PM format. I know there is a little bit of redundancy, but just a quick mod on my part.

((1+[012]+)|((0+[123456789]{1}))|([123456789]{1}))(((:|\s)[0-5]+[0-9]+))?(\s)?((a|A|p|P)(m|M)?)

# re: Regular expression for Time of day 8/12/2009 6:23 AM Deven Sawant
Hey thanks bro its work like charm

# re: Regular expression for Time of day 9/22/2009 10:32 AM Anders Persson
Not working exact to my specs, here is what I am using currently that does the ebst job so far:
(^([0-9]|[0-1][0-9]|[2][0-3]):([0-5][0-9])(\s{0,1})(AM|PM|am|pm|aM|Am|pM|Pm{2,2})$)|(^([0-9]|[1][0-9]|[2][0-3])(\s{0,1})(AM|PM|am|pm|aM|Am|pM|Pm{2,2})$)

# re: Regular expression for Time of day 9/15/2010 4:37 AM Bezo
Regular expression for time format hhmm

# re: Regular expression for Time of day 7/29/2011 12:44 PM rich
How do you express twelve noon numerically? Is it, "12:00 p.m." or "12:00 a.m."?

Thank you.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: