moment.js isValid function not working properly

前端 未结 2 1566
北海茫月
北海茫月 2020-12-01 04:10

I have this question... I haven\'t found anything similar and it also seems very strange that nobody had this problem validating time with moment.js.

moment(         


        
相关标签:
2条回答
  • 2020-12-01 04:49

    Sorry to necro this 5 year old question, but I indeed stumbled upon a case where monent is not working properly towards the documentation, using version 2.24.0.

    In the picture we can see that for example H should only evaluate to 0 - 23, but if I use moment('01', 'H', true).isValid() I still get true.

    Here is the jsfiddle: https://jsfiddle.net/wofgst5v/

    0 讨论(0)
  • 2020-12-01 04:51

    In your question you write that moment('03:55jojojo', 'HH:mm',true).isValid(); returns true. This is incorrect. Please check your jsfiddle again.

    From http://momentjs.com/docs/

    Moment's parser is very forgiving, and this can lead to undesired behavior. As of version 2.3.0, you may specify a boolean for the last argument to make Moment use strict parsing. Strict parsing requires that the format and input match exactly.

    moment('It is 2012-05-25', 'YYYY-MM-DD').isValid();        // true
    moment('It is 2012-05-25', 'YYYY-MM-DD', true).isValid();  // false
    moment('2012-05-25', 'YYYY-MM-DD', true).isValid();        // true
    

    You can use both language and strictness.

    moment('2012-10-14', 'YYYY-MM-DD', 'fr', true);
    
    0 讨论(0)
提交回复
热议问题