Regular Expression for MM/DD/YYYY in Javascript

后端 未结 7 1073
青春惊慌失措
青春惊慌失措 2020-12-17 20:47

I\'ve just written this regular expression in javaScript however it doesn\'t seem to work, here\'s my function:

function isGoodDate(dt){
    var reGoodDate =         


        
相关标签:
7条回答
  • 2020-12-17 21:24

    Attention, before you copy+paste: The question contains some syntactic errors in its regex. This answer is correcting the syntax. It is not claiming to be the best regex for date/time parsing.

    Try this:

    function isGoodDate(dt){
        var reGoodDate = /^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/;
        return reGoodDate.test(dt);
    }
    

    You either declare a regular expression with:

    new RegExp("^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$")
    

    Or:

    /^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/
    

    Notice the /

    0 讨论(0)
  • 2020-12-17 21:26

    I agree with @KooiInc, but it is not enough to test for NaN

    function isGoodDate(dt){
        var dts  = dt.split('/').reverse()
           ,dateTest = new Date(dts.join('/'));
        return !isNaN(dateTest) && 
           dateTest.getFullYear()===parseInt(dts[0],10) &&
           dateTest.getMonth()===(parseInt(dts[1],10)-1) &&
           dateTest.getDate()===parseInt(dts[2],10) 
    }
    

    which will handle 29/2/2001 and 31/4/2011


    For this script to handle US dates do

    function isGoodDate(dt){
        var dts  = dt.split('/')
           ,dateTest = new Date(dt);
        return !isNaN(dateTest) && 
           dateTest.getFullYear()===parseInt(dts[2],10) &&
           dateTest.getMonth()===(parseInt(dts[0],10)-1) &&
           dateTest.getDate()===parseInt(dts[1],10) 
    }
    
    0 讨论(0)
  • 2020-12-17 21:29

    Try the below code which accepts following date formats:

    MM-DD-YYYY, MM-DD-YY, DD-MM-YYYY, DD-MM-YY, MM/DD/YYYY, MM/DD/YY, DD/MM/YYYY, DD/MM/YY, MM\DD\YYYY, MM\DD\YY, DD\MM\YYYY, DD\MM\YY

    function isGoodDate(dt) {
        var reGoodDate = /(?:((0\d|[12]\d|3[01])|(0\d|1[012]))[\-|\\|\/]((0\d|1[012])|(0\d|[12]\d|3[01]))[\-|\\|\/](((19|20)\d{2})|\d\d))/;
        return reGoodDate.test(dt);
    }
    
    0 讨论(0)
  • 2020-12-17 21:35

    I don't think you need a regular expression for this. Try this:

    function isGoodDate(dt){
        var dts  = dt.split('/').reverse()
           ,dateTest = new Date(dts.join('/'));
        return isNaN(dateTest) ? false : true;
    }
    
    //explained
        var dts  = dt.split('/').reverse()
    //      ^ split input and reverse the result
    //        ('01/11/2010' becomes [2010,11,01]
    //        this way you can make a 'universal' 
    //        datestring out of it
           ,dateTest = new Date(dts.join('/'));
    //     ^ try converting to a date from the 
    //       array just produced, joined by '/'
        return isNaN(dateTest) ? false : true;
    //         ^ if the date is invalid, it returns NaN
    //           so, if that's the case, return false
    
    0 讨论(0)
  • 2020-12-17 21:37

    Add this in your code, it working perfectly fine it here. click here http://jsfiddle.net/Shef/5Sfq6/

    function isGoodDate(dt){
    var reGoodDate = /^(?:(0[1-9]|1[012])[\/.](0[1-9]|[12][0-9]|3[01])[\/.](19|20)[0-9]{2})$/;
    return reGoodDate.test(dt);
    

    }

    0 讨论(0)
  • 2020-12-17 21:41

    Maybe because you are declaring the isGoodDate() function, and then you are calling the isCorrectDate() function?

    Try:

    function isGoodDate(dt){
        var reGoodDate = /^(?:(0[1-9]|1[012])[\/.](0[1-9]|[12][0-9]|3[01])[\/.](19|20)[0-9]{2})$/;
        return reGoodDate.test(dt);
    }
    

    Works like a charm, test it here.

    Notice, this regex will validate dates from 01/01/1900 through 31/12/2099. If you want to change the year boundaries, change these numbers (19|20) on the last regex block. E.g. If you want the year ranges to be from 01/01/1800 through 31/12/2099, just change it to (18|20).

    0 讨论(0)
提交回复
热议问题