How to write regex to validate dates?

I'm working in JavaScript and I need to figure out how to determine a valid date using regular expressions.

The matches will be:

dd-mm-yyyy
dd-mm-yy

Also, no leading zeros should be accepted like:

9-8-2010
10-6-99

How can I write a regular expression to do this?


Solution 1:

I came up with this:

function isValidDate(inputDate){

    var myRegex = /^(\d{1,2})([\-\/])(\d{1,2})\2(\d{4}|\d{2})$/;
    var match = myRegex.exec(inputDate);

    if (match != null) {
        var auxDay = match[1];
        var auxMonth = match[3] - 1;
        var auxYear = match[4];
        auxYear = auxYear.length < 3 ? (auxYear < 70 ? '20' + auxYear : '19' + auxYear) : auxYear;
        var testingDate = new Date(auxYear,auxMonth,auxDay);
        return ((auxDay == testingDate.getDate()) && (auxMonth == testingDate.getMonth()) && (auxYear == testingDate.getFullYear()));
    } else return false;
}

Works for dd-mm-yyyy, dd-mm-yy, d-m-yyyy and d-m-yy, using - or / as separators

Based on This script

Solution 2:

You'd better do a split on - and test all elements. But if you really want to use a regex you can try this one :

/^(?:(?:31-(?:(?:0?[13578])|(1[02]))-(19|20)?\d\d)|(?:(?:29|30)-(?:(?:0?[13-9])|(?:1[0-2]))-(?:19|20)?\d\d)|(?:29-0?2-(?:19|20)(?:(?:[02468][048])|(?:[13579][26])))|(?:(?:(?:0?[1-9])|(?:1\d)|(?:2[0-8]))-(?:(?:0?[1-9])|(?:1[0-2]))-(?:19|20)?\d\d))$/

Explanation:

^            # start of line
 (?:         # group without capture
             # that match 31st of month 1,3,5,7,8,10,12
   (?:       # group without capture
     31      # number 31
     -       # dash
     (?:     # group without capture
       (?:   # group without capture
         0?  # number 0 optionnal
         [13578] # one digit either 1,3,5,7 or 8
       )     # end group
       |     # alternative
       (1[02]) # 1 followed by 0,1 or 2
     )       # end group
     -       # dash
     (19|20)? #numbers 19 or 20 optionnal
     \d\d    # 2 digits from 00 to 99 
   )         # end group
|
   (?:(?:29|30)-(?:(?:0?[13-9])|(?:1[0-2]))-(?:19|20)?\d\d)
|
   (?:29-0?2-(?:19|20)(?:(?:[02468][048])|(?:[13579][26])))
|
   (?:(?:(?:0?[1-9])|(?:1\d)|(?:2[0-8]))-(?:(?:0?[1-9])|(?:1[0-2]))-(?:19|20)?\d\d)
 )
$

I've explained the first part, leaving the rest as an exercise.

This match one invalid date : 29-02-1900 but is correct for any date between 01-01-1900 and 31-12-2099

Solution 3:

To validate yyyy-mm-dd hh:MM simplistically (24 hr time):

var dateFormat=/^20\d{2}-(0[1-9]|1[0-2])-[0-3]\d\s([0-1][0-9]|2[0-3]):[0-5]\d$/;
var myDate="2017-12-31"; 
if ( myDate.match(dateFormat)){
    console.log('matches');
};

Changing the first term ( 20\d{2} ) to \d{4} will allow any 4-digit year, including 0000 and 9999. Also, this regex forces leading zeroes for month, day, hours, and minutes.

This regex checks for:

  • the year to be 20xx; change to your preference.
  • leading zeroes for month, day, hours, and minutes

This regex doesn't check for:

  • Month / day accuracy (it will allow Feb 30 and June 31, for example)
  • Leap years
  • AM or PM (it's for 24-hour time)