If you have a PH account, you can customize your PH profile.

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])

 

Other Views

corner
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - 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 our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A bootstrapLabs project.