DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Reinis has posted 8 posts at DZone. View Full User Profile

Regular Expression To Match MySQL's DATETIME Type

06.12.2007
| 12174 views |
  • submit to reddit
        Matches MySQL built-in datetime type, for example, 2007-06-05 15:26:02
It also matches date type, i.e. 2007-06-05

/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/
    

Comments

Snippets Manager replied on Thu, 2012/01/12 - 2:44am

Would just like to add to the code.Adding some more validation on month,day and hour end ([0-9]{2,4})-([0][0-9]|1[0-2])-([0-2][0-9]|3[0-1]) (?:([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))? This will now not allow dates format 2011-19-32 28:00:00 .....