Skip to content

Validate a date in javascript

January 9, 2007

The below code helps to validate a date(mm-dd-yyyy) with seperators being any one of “-“, “/”, “.”. Example “10-23-1979” or “03/04/1979” or “02.29.1900”.

var dateObj=”02.29.2000″

var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
var errorMessage1 =’Invalid date’
if (!(dateObj.value.match(RegExPattern)) && (dateObj.value!=”)) {
return false;

Also the below gets the difference between 2 dates:
var sDate = new Date(“09/23/2006”);
var eDate = new Date(“01/01/2000”);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));



From → javascript

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: