Regular Expression to match valid dates

后端 未结 15 2093
庸人自扰
庸人自扰 2020-11-22 04:48

I\'m trying to write a regular expression that validates a date. The regex needs to match the following

  • M/D/YYYY
  • MM/DD/YYYY
  • Single digit mon
15条回答
  •  情书的邮戳
    2020-11-22 05:18

    A slightly different approach that may or may not be useful for you.

    I'm in php.

    The project this relates to will never have a date prior to the 1st of January 2008. So, I take the 'date' inputed and use strtotime(). If the answer is >= 1199167200 then I have a date that is useful to me. If something that doesn't look like a date is entered -1 is returned. If null is entered it does return today's date number so you do need a check for a non-null entry first.

    Works for my situation, perhaps yours too?

提交回复
热议问题