Andrew
Andrew

Reputation: 4441

MM/DD (Including Leap Year) RegEx Pattern

It took me a while to find the answer to MM/DD including Leap Year (Feb 29th).

So I figured I'd make a question and answer my own question for other's to view.

Upvotes: 0

Views: 83

Answers (2)

Amarnath Balasubramanian
Amarnath Balasubramanian

Reputation: 9460

Reg ex that matches all valid dates including leap years. Formats accepted mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy format

Flow for how this works

  ^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

Upvotes: 1

Andrew
Andrew

Reputation: 4441

Use the following

^((0?[1-9]|1[012])\/(0?[1-9]|1\d|2[0-9])|(0?[13456789]|1[012])\/(29|30)|(0?[13578]|1[02])\/31)$

Upvotes: 1

Related Questions