moment.js validating invalid date “2013-10-311”

后端 未结 3 371
北恋
北恋 2021-01-04 02:33

Running moment.js, 2.2.1

moment(\"2010-10-319\", [\"YYYY-MM-DD\"]).isValid()

... returns true, and the moment object would be

相关标签:
3条回答
  • 2021-01-04 03:03

    Moment.js version 2.3.0 added strict parsing.

    moment("2010-10-319", ["YYYY-MM-DD"]).isValid();       // true
    moment("2010-10-319", ["YYYY-MM-DD"], true).isValid(); // false
    
    moment("2010-10-31a", ["YYYY-MM-DD"]).isValid();       // true
    moment("2010-10-31a", ["YYYY-MM-DD"], true).isValid(); // false
    
    var formats = ["MM/DD/YYYY", "MM-DD-YYYY", "YYYY-MM-DD"];
    
    moment("2010-10-319",  formats).isValid(); // true
    moment("2010-10-3199", formats).isValid(); // false
    
    moment("2010-10-319",  formats, true).isValid(); // false
    moment("2010-10-3199", formats, true).isValid(); // false
    
    moment("2010-10-319qr", formats).isValid();       // true
    moment("2010-10-319qr", formats, true).isValid(); // false
    
    0 讨论(0)
  • 2021-01-04 03:20

    create an Issue on the Git Repository from momentjs https://github.com/moment/moment/ the best way to handle this error.

    0 讨论(0)
  • 2021-01-04 03:25

    If the user is not selecting any date, then it is showing as invalid date. It is wrong, it should show no date or date not selected. To change that in moment.js you can change that to nodate instead of invalid date.

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