Why is moment.js allowing date with one digit year when using strict parsing?

你离开我真会死。 提交于 2019-12-04 04:20:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!