Im looking for regex for datetime in this pattern: 'MM/dd/yyyy hh:mm:ss' and the time is optional. Expected output for example:
regex.test(datestring)
"10/02/2020" true
"10/02/2020 12:00:12" true
"2000/01/05" false
"10-02-2020" false
I found this regexp that works on [test regex online][1].
^(0?[1-9]|[12][0-9]|3[01])[\/](0?[1-9]|1[012])[\/]\d{4}( \d{1,2}[:]\d{2}([:]\d{2,3})*)?
This is my code:
const d = new Date(curruntRecord[1].trim());
const regex = RegExp('^(0?[1-9]|[12][0-9]|3[01])[\/](0?[1-9]|1[012])[\/]\d{4}( \d{1,2}[:]\d{2}([:]\d{2,3})*)?');
if ( isNaN(d.getTime()) || (!regex.test(curruntRecord[1]) )) {
this.invalidData = true;
this.fileReset();
alert ('This file contains invalid dates, please import another file.' + curruntRecord[1] );
break;
}
Any solution for me?
[1]: https://regex101.com/`enter code here`