Regular Expression to validate a timestamp

后端 未结 13 547
名媛妹妹
名媛妹妹 2021-01-01 01:19

I need a regular expression to validate a timestamp of the format, using Javascript:

YYYY/MM/DD HH:MI:SS

I trie

13条回答
  •  时光说笑
    2021-01-01 02:01

    I just wrote a Regex to control a MySQL datetime (and I think that it's not so different for Oracle or other database server).

    This ReGex is very secure and controls the validity of the date (months in 28, 30 & 31 days, and even years with 29/02). Then, it controls the time. The timestamp format is this one :

    YYYY-MM-DD HH:MM:SS

    Here is the Regex (quite long) :

    ((((19|20)([2468][048]|[13579][26]|0[48])|2000)-02-29|((19|20)[0-9]{2}-(0[4678]|1[02])-(0[1-9]|[12][0-9]|30)|(19|20)[0-9]{2}-(0[1359]|11)-(0[1-9]|[12][0-9]|3[01])|(19|20)[0-9]{2}-02-(0[1-9]|1[0-9]|2[0-8])))\s([01][0-9]|2[0-3]):([012345][0-9]):([012345][0-9]))
    

    Matches

    2013-04-05 17:59:59 | 2013-07-30 01:22:42 | 2099-12-30 23:59:59 | 2016-02-28 00:00:00

    Non-Matches

    2016-02-29 -01:01:02 | 3000-04-24 17:42:21 | 2012-03-03 24:24:02 | 2012-03-03 21:60:45

    I don't think I can make a more secured one. Very functionnel and tested. Please, if you use it, don't hesitate to give me feedback ;)

    FYI : If you just look for the date Regex control without time, please go there to find it : Regular Expression to match valid dates (check my post).

    Cheers

提交回复
热议问题