开发者

How to use Regular Expression to format a date?

开发者 https://www.devze.com 2023-02-02 05:40 出处:网络
I am developing a application in ASP.NET and my date format is dd-mm-yyyy so therefore required a h开发者_StackOverflowelp to build regular expression for it.Try this from regular-expressions.info:

I am developing a application in ASP.NET and my date format is dd-mm-yyyy so therefore required a h开发者_StackOverflowelp to build regular expression for it.


Try this from regular-expressions.info:

^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$

It's good to use a tool such as Nregex or RegExr when you're trying out regular expressions.


from DotNetSlackers, regex for dd/mm/yyyy:

^(((((0[1-9])|(1\d)|(2[0-8]))\/((0[1-9])|(1[0-2])))|((31\/((0[13578])|(1[02])))|((29|30)\/((0[1,3-9])|(1[0-2])))))\/((20[0-9][0-9])|(19[0-9][0-9])))|((29\/02\/(19|20)(([02468][048])|([13579][26]))))$

Modified to work with dd-mm-yyyy

^(((((0[1-9])|(1\d)|(2[0-8]))-((0[1-9])|(1[0-2])))|((31-((0[13578])|(1[02])))|((29|30)-((0[1,3-9])|(1[0-2])))))-((20[0-9][0-9])|(19[0-9][0-9])))|((29-02-(19|20)(([02468][048])|([13579][26]))))$
0

精彩评论

暂无评论...
验证码 换一张
取 消