Regular Expressions Library :: Dates And Times

This section of the library contains regular expressions for matching dates and times. It is possible to write very loose checks that simply look for the presence of numbers between colons or dashes. The examples here attempt to be more sophisticated. To match a string that matches one of these expressions as a whole rather than in part, put a ^ before the expression and a $ after it. For example, \d+ becomes ^\d+$.

24-Hour HH:MM:SS

Description: Matches a 24 hour time in the format HH:MM:SS, capturing the hours, minutes and seconds.
Perl Compatible Form: ([0-1]\d|2[0-3]):([0-5]\d):([0-5]\d)
POSIX Form: ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])

24-Hour HH:MM:SS With Optional Leading Zeroes

Description: Matches a 24 hour time in the format HH:MM:SS, capturing the hours, minutes and seconds. Will also match if there are no leading zeroes, that is 2:43:55 will be matched as well as 02:43:55. To only make the leading zero on the hour optional, remove the ? after each [0-5].
Perl Compatible Form: ([0-1]?\d|2[0-3]):([0-5]?\d):([0-5]?\d)
POSIX Form: ([0-1]?[0-9]|2[0-3]):([0-5]?[0-9]):([0-5]?[0-9])

12-Hour HH:MM:SS

Description: Matches a 12 hour time in the format HH:MM:SS, capturing the hours, minutes and seconds.
Perl Compatible Form: (0\d|1[0-2]):([0-5]\d):([0-5]\d)
POSIX Form: (0[0-9]|1[0-2]):([0-5][0-9]):([0-5][0-9])

12-Hour HH:MM:SS With Optional Leading Zeroes

Description: Matches a 12 hour time in the format HH:MM:SS, capturing the hours, minutes and seconds. Will also match if there are no leading zeroes, that is 2:43:55 will be matched as well as 02:43:55. To only make the leading zero on the hour optional, remove the ? after each [0-5].
Perl Compatible Form: (0?\d|1[0-2]):([0-5]?\d):([0-5]?\d)
POSIX Form: (0?[0-9]|1[0-2]):([0-5]?[0-9]):([0-5]?[0-9])

YYYY-MM-DD (Loose)

Description: Matches something that looks like a date in YYYY-MM-DD format. It does not take account of number of days in a month and certainly doesn't have any constraints relating to leap years.
Perl Compatible Form: (\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])
POSIX Form: ([0-9][0-9][0-9][0-9])-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])

DD-MM-YYYY (Loose)

Description: Matches something that looks like a date in DD-MM-YYYY format. It does not take account of number of days in a month (but does limit it to no more than 31) and certainly doesn't have any constraints relating to leap years.
Perl Compatible Form: (0[1-9]|[12]\d|3[01])-(0[1-9]|1[0-2])-(\d{4})
POSIX Form: (0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[0-2])-([0-9][0-9][0-9][0-9])

MM-DD-YYYY (Loose)

Description: Matches something that looks like a date in MM-DD-YYYY format. It does not take account of number of days in a month (but does limit it to no more than 31) and certainly doesn't have any constraints relating to leap years.
Perl Compatible Form: (0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])-(\d{4})
POSIX Form: (0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])-([0-9][0-9][0-9][0-9])

YYYY-MM-DD (Tight)

Description: Matches a date in the format YYYY-MM-DD. It does take account of the number of days that certain months have, but does not do leap year detection so the 29th of February will always match no matter which year it is. A small bit of additional application code can be used to check this.
Perl Compatible Form: (\d{4})-(0[13578]|1[02])-(0[1-9]|[12]\d|3[01])|(\d{4})-(0[469]|11])-
(0[1-9]|[12]\d|30)|(\d{4})-(02)-(0[1-9]|1\d|2[0-9])
POSIX Form: ([0-9][0-9][0-9][0-9])-(0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])|
([0-9][0-9][0-9][0-9])-(0[469]|11])-(0[1-9]|[12][0-9]|30)|([0-9]{4})-(02)-
(0[1-9]|1[0-9]|2[0-9])

YYYY-MM-DD (With Leap Year Detection)

Description: This is probably best done with application level logic, but it is possible to write a regular expression that basically works. This one will work up until the year 2100, when it will incorrectly detect that year as a leap year. It is possible to fix that, but it would make this already huge regular expression even bigger.
Perl Compatible Form: (\d{4})-(0[13578]|1[02])-(0[1-9]|[12]\d|3[01])|
(\d{4})-(0[469]|11])-(0[1-9]|[12]\d|30)|
(\d\d[0248][048]|\d\d[13579][26])-(02)-(0[1-9]|1\d|2\d)|
(\d\d[0248][1235679]|\d\d[13579][01345789])-(02)-(0[1-9]|1\d|2[0-8])
POSIX Form: ([0-9][0-9][0-9][0-9])-(0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])|
([0-9][0-9][0-9][0-9])-(0[469]|11])-(0[1-9]|[12][0-9]|30)|
([0-9][0-9][0248][048]|[0-9][0-9][13579][26])-(02)-(0[1-9]|1[0-9]|2[0-9])|
([0-9][0-9][0248][1235679]|[0-9][0-9][13579][01345789])-(02)-(0[1-9]|1[0-9]|2[0-8])

  User Comments


Anonymous
(Not rated)
(Report as abusive)
new String("123")????
new String(<literal>) is a newbie mistake... just use the literal directly.
Anonymous

(Report as abusive)
the comment above is incorrect
It is true that declaring variables unnecessarily is a frequent newbie mistake. However, in this case, it is necessary to have a String object to operate on.

For example, if you don't put the string in var text here, you can't use the replace method on it:

var text = "abababab";
var altered = text.replace(/b/, 'a');

Would you write that as
var altered = "abababab".replace(/b/, 'a');

I think not.

However, I do think that there is a typo in the telephone number example. The string is placed into a variable named "phone", so the second line should probably be:
var lastfour = phone.match(/\d{4}$/);

instead of
var lastfour = text.match(/\d{4}$/);

which (probably inadvertently) references a variable named "text" which is used in adjacent examples.
  View all   Rate and comment this article




 
Printer friendly version of the RegexLibraryDateTime page


Sponsored links

Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
SFTP components for .NET
Add complete SSH and SFTP support to your .NET framework application
Virtual File System SDK
Create your own file systems in Windows and .NET applications
PureCM Software Configuration Management
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.