Does anyone know of a reg expression for uk date format

后端 未结 7 984
青春惊慌失措
青春惊慌失措 2021-01-18 21:39

Hi does any one know a reg ex for a uk date format e.g. dd/mm/yyyy.

The dd or mm can be 1 character e.g. 1/1/2010 but the year must always be 4 characters.

T

7条回答
  •  长发绾君心
    2021-01-18 22:27

    Regex is not the right tool for this job.

    It is very difficult (but possible) to come up with the regex to match a valid date. Things like ensuring Feb has 29 days on leap year and stuff is not easily doable in regex.

    Instead check if your language library provides any function for validating dates.

    PHP has one such function called checkdate :

    bool checkdate  ( int $month  , int $day  , int $year)
    

提交回复
热议问题