问题
I am using moment.js to parse a date. http://momentjs.com/docs/#/parsing/string-format/
This is to validate and convert the date to a format required by my database. In my testing I encountered this input date '6-4-3' which should not be valid with the given format.
moment('6-4-3','YYYY-MM-DD',true).isValid(); //returns true
The resulting date in the moment object is "Mon Apr 03 0006 ...". When I call isValid, it returns true. I think it should consider this date invalid because it has too few digits in the input for each part of the format string.
I added a regex.test in my code to ensure that the dates have the right number of digits, but I think this should be handled differently in moment.js when strict is true.
Is there something I'm missing here? Isn't this the point of strict parsing? Or is there a reason why this is the intended behavior?
回答1:
This was a known issue for past versions of Moment.js: YYYY-MM-DD with 3 digit year returns true for isValid().
It was fixed in December 2013, and your code works as expected with the most recent version of the library. Version 2.3.1 was released in October 2013 and does not include this fix.
来源:https://stackoverflow.com/questions/25892298/why-is-moment-js-allowing-date-with-one-digit-year-when-using-strict-parsing