Skip to content Skip to sidebar Skip to footer

Mm/dd/yyyy Hh:mm:ss Am/pm Date Validation Regular Expression In Javascript

I am trying to validate a date in format MM/DD/YYYY HH:MM:SS AM/PM in javascript. I am unable to find any good answer tried other results but did not work. This issue has been as

Solution 1:

Date validation with regular expressions in JavaScript.

The expressions below validate any date from 01-01-1900 00:00:00 AM.

Date format: mm[/.-]dd[/.-]yyyy hh:mm:ss am|pm

var r = /^(((0[13578]|1[02])[\/\.-](0[1-9]|[12]\d|3[01])[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((0[13456789]|1[012])[\/\.-](0[1-9]|[12]\d|30)[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((02)[\/\.-](0[1-9]|1\d|2[0-8])[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((02)[\/\.-](29)[\/\.-]((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm)))$/g;

r.test('06/12/2014 12:45:56 AM'); //true
r.test('11-12-1965 06:04:34 PM'); //true
r.test('11/31/2015 11:40:00 AM'); //false
r.test('12-31-1945 01:38:09 PM'); //true
r.test('02/29/2012 09:04:10 AM'); //true [leap year]
r.test('02/29/2013 09:04:10 AM'); //false
r.test('06.12.2014 13:04:10 AM'); //false

Date format: dd[/.-]mm[/.-]yyyy hh:mm:ss am|pm

var r = /^(((0[1-9]|[12]\d|3[01])[\/\.-](0[13578]|1[02])[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((0[1-9]|[12]\d|30)[\/\.-](0[13456789]|1[012])[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((0[1-9]|1\d|2[0-8])[\/\.-](02)[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((29)[\/\.-](02)[\/\.-]((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm)))$/g;

r.test('12/06/2014 12:45:56 AM'); //true
r.test('12-11-1965 06:04:34 PM'); //true
r.test('31/11/2015 11:40:00 AM'); //false
r.test('21-12-1945 01:38:09 PM'); //true
r.test('29/02/2012 09:04:10 AM'); //true [leap year]
r.test('29/02/2013 09:04:10 AM'); //false
r.test('12.06.2014 13:04:10 AM'); //false

Date format: dd[/.-]mm[/.-]yyyy hh:mm:ss

var r = /^(((0[1-9]|[12]\d|3[01])[\/\.-](0[13578]|1[02])[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))|((0[1-9]|[12]\d|30)[\/\.-](0[13456789]|1[012])[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))|((0[1-9]|1\d|2[0-8])[\/\.-](02)[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))|((29)[\/\.-](02)[\/\.-]((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])))$/g;

r.test('24/06/2014 15:08:05'); //true
r.test('29/02/2016 23:19:20'); //true [leap year]
r.test('29/02/2015 23:19:20'); //false
r.test('31/11/2010 10:00:02'); //false

Date format: yyyy[/.-]mm[/.-]dd hh:mm:ss

var r = /^((((19|[2-9]\d)\d{2})[\/\.-](0[13578]|1[02])[\/\.-](0[1-9]|[12]\d|3[01])\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))|(((19|[2-9]\d)\d{2})[\/\.-](0[13456789]|1[012])[\/\.-](0[1-9]|[12]\d|30)\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))|(((19|[2-9]\d)\d{2})[\/\.-](02)[\/\.-](0[1-9]|1\d|2[0-8])\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))[\/\.-](02)[\/\.-](29)\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])))$/g;

r.test('2014-06-24 15:49:05'); //true
r.test('2016-02-29 23:19:20'); //true [leap year]
r.test('2015/02/29 23:19:20'); //false
r.test('2010.11.31 10:00:02'); //false
r.test('1956.10.12 24:10:02'); //false

Post a Comment for "Mm/dd/yyyy Hh:mm:ss Am/pm Date Validation Regular Expression In Javascript"