user342944
user342944

Reputation: 418

How to use Regular Expression to format a date?

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

Upvotes: 0

Views: 71

Answers (2)

Sandwich
Sandwich

Reputation: 2289

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]))))$

Upvotes: 0

Joe Ratzer
Joe Ratzer

Reputation: 18569

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.

Upvotes: 1

Related Questions