Regex to Match Mm/Dd/Yyyy Hh:Mm:Ss Am or Pm

MM/DD/YYYY HH:MM:SS AM/PM date validation regular expression in javascript

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

Regex to match mm/dd/yyyy hh:mm:ss AM or PM

This regex should work for you for validation of your inputs:

^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{1,2}:\d{1,2} [AP]M\z

RegEx Demo

This will validate either of these inputs lines:

10/22/2016 9:15:32 PM
9/9/2016 9:9:5 AM

Regex for mm/dd/yyyy hh:mm AM/PM

Though it could probably be done with a regex, it would become complicated and unreadable, and I personally don't think that it would be the best solution here. A leap year is more complex than 'is divisible by 4' for instance. 1900 was no leap year, and neither will 2100...

I would work with my favorite date js toolbox in your case, moment.js

The code would look something like this:

moment(myDate, 'M/D/YYYY h:mm a').isValid();

Note that moment.js provides a ton of other, very useful functions. You could for instance cast your date to a timestamp before sending it to your backend, or even perform calculations with your dates if that would be desired. Just have a look at the docs, I promise you you'll love this toolbox!

regular expression for dd-mm-yyyy hh:mm

Here is a Regexp for such date format.

But be aware that it accepts both dates 1-12-2011 19:20 and 01-12-2011 19:20.

^([1-9]|([012][0-9])|(3[01]))-([0]{0,1}[1-9]|1[012])-\d\d\d\d [012]{0,1}[0-9]:[0-6][0-9]$

Regex function to validate ' MM/DD/YYYY hh:mm' in python

Your regex is trying to match YYYY-MM-DD for the date. And it is also expecting a 24-hour time of the format HH:MM:SS when your data only has hours and minutes and uses AM/PM (and so, hours will run from 01 to 12 not 00 to 23).

This regex will do what you want: \d?\d/\d?\d/\d{4} [0-1][0-9]:[0-5][0-9] ([AP]M)

But you should not use a regex for this, because the validation it performs will be short of the mark. This regex will match 31 February as if it were correct, and it isn't.

Use datetime.datetime.strptime() instead, with format %m/%d/%Y %I:%M %p.

>>> datetime.datetime.strptime("01/02/2020 05:25 AM", "%m/%d/%Y %I:%M %p")
datetime.datetime(2020, 1, 2, 5, 25)

This is better because strptime() knows about months with less than 31 days, and leap years.

>>> datetime.datetime.strptime("02/29/2020 05:25 AM", "%m/%d/%Y %I:%M %p")
datetime.datetime(2020, 2, 29, 5, 25)
>>> datetime.datetime.strptime("02/29/2021 05:25 AM", "%m/%d/%Y %I:%M %p")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "E:\Python27\lib\_strptime.py", line 458, in _strptime
datetime_date(year, 1, 1).toordinal() + 1
ValueError: day is out of range for month

This shows you that you need to put a try...except ValueError block around your strptime() call to trap invalid dates.

And, besides validation, why else should you convert your string to a datetime? Well, because the next thing you will want to do is store the date in a variable for processing, or in a database for storage. You're not planning to store it as a string, are you?

How to write a regex for MM:DD:YYYY:HH:MM:SS

If you want to capture the values for month, year, ... you could use something like this, I suppose :

([0-9]{2}):([0-9]{2}):([0-9]{4}):([0-9]{2}):([0-9]{2}):([0-9]{2})


If supported by your regex engine, \d can be used as an alias to [0-9] :

(\d{2}):(\d{2}):(\d{4}):(\d{2}):(\d{2}):(\d{2})


Related Topics



Leave a reply



Submit