moment.js验证无效日期“2013-10-311” [英] moment.js validating invalid date "2013-10-311"

查看:2852
本文介绍了moment.js验证无效日期“2013-10-311”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行moment.js,2.2.1

Running moment.js, 2.2.1

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

...返回 true ,时刻对象将设置为2010年10月31日。解析器似乎会剥离任何类型的无关字符:

... returns true, and the moment object would be set to 31 October 2010. The parser seems to strip extraneous characters of any sort:

moment("2010-10-31a", ["YYYY-MM-DD"]).isValid(); // true

好奇,如果您添加其他格式选项,则剥离仅限于仅一个字符! (格式字符串测试不应该是ORed?)

Curiouser, if you add additional format choices, then the "stripping" becomes limited to only one character! (Shouldn't the format strings tests be ORed?)

moment("2010-10-319", ["MM/DD/YYYY", "MM-DD-YYYY", "YYYY-MM-DD"]).isValid(); // true
moment("2010-10-3199", ["MM/DD/YYYY", "MM-DD-YYYY", "YYYY-MM-DD"]).isValid(); // false (!!!)

设计是否有这种行为?我不知道为什么。

Is this behaviour by design? I'm not getting why.

编辑:评论者发现另外一种情况是超出一个的额外字符确实被剥夺了:

A commenter found another case where extra characters beyond one are, indeed, stripped:

moment("2010-10-319qr", ["MM/DD/YYYY", "MM-DD-YYYY", "YYYY-MM-DD"]).isValid(); // true (!)

这是在行动:http://jsfiddle.net/grahampcharles/r42jg/6/ (用新的案例更新)

Here is is in action: http://jsfiddle.net/grahampcharles/r42jg/6/ (updated with new case)

推荐答案

从timjs创建Git Repository上的问题 https:// github。 com / moment / moment / 处理此错误的最佳方式。

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

这篇关于moment.js验证无效日期“2013-10-311”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆