How to check if a string is a legal “dd/mm/yyyy” date?

前端 未结 7 1944
遥遥无期
遥遥无期 2020-12-01 15:05

Given a string str, how could I check if it is in the dd/mm/yyyy format and contains a legal date ?

Some examples:

bla bla          


        
7条回答
  •  独厮守ぢ
    2020-12-01 15:32

    I'm going to answer a different question, as Misha Moroshko's has already been well-answered: use HTML5. That is, on the assumption that the strings in question arise as user inputs through a Web browser, I propose that the entries be received as

       

    I recognize that not all browsers likely to be in use will interpret "date" in a way that rigorously enforces validity. It's the right thing to do, though, will certainly improve as time goes on, and might well be good enough in a particular context even now simply to eliminate the need to validate the date-string after the fact.

提交回复
热议问题