Good morning everyone.
I was looking for a while for some regular expression to validate the date and time format, dd/mm/yyyy hh:mm or dd-mm-yyyy hh:mm or similar, but the ones I found were incomplete or only partially met the conditions or other date formats.
I leave you the base from which I am doing tests. Currently only valid for dates:
( https://regex101.com/r/yVN3p2/1 )
^([0][1-9]|[12][0-9]|3[01])(\/|-)([0][1-9]|[1][0-2])\2(\d{4})(\s)([0-1][1-9]|[2][0-3])(:)([0-5][0-9])$
Taking the regular expression from the question as a base we can create the following validations:
Date: dd/mm/yyyy or dd-mm-yyyy
https://regex101.com/r/yVN3p2/7
Date, hour and minute: dd/mm/yyyy hh:mm or dd-mm-yyyy hh:mm
https://regex101.com/r/yVN3p2/9
Date, hour, min, sec: dd/mm/yyyy hh:mm:ss or dd-mm-yyyy hh:mm:ss
https://regex101.com/r/yVN3p2/10
An example of validation with PHP could be the following
I made 2 regular expressions that validate the dates, they are improved, below I leave more documentation.
Date validation in format: YYYY-MM-DD, this regular expression does not allow erroneous dates such as February 30 and 31 or February 29 in a year that was not or will be a Leap:
Date validation in format: YYYY-MM-DDTHH:MM:SS, this regular expression does not allow erroneous dates such as February 30 and 31 or February 29 in a year that was not or will be a Leap, the hour format is 24 hours:
These two regular expressions are a combination of the following regular expressions:
Validation of time format 24 hours without seconds:
Validation of time format 24 hours with seconds:
Validation of time format 12 hours without seconds:
Validation of time format 12 hours with seconds: