在使用严格解析时,moment.js为什么允许日期以一位数字表示? [英] Why is moment.js allowing date with one digit year when using strict parsing?

查看:104
本文介绍了在使用严格解析时,moment.js为什么允许日期以一位数字表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用moment.js来解析日期. http://momentjs.com/docs/#/parsing/string-format/

I am using moment.js to parse a date. http://momentjs.com/docs/#/parsing/string-format/

这是为了验证日期并将其转换为数据库所需的格式.在我的测试中,我遇到了输入日期"6-4-3",该日期对于给定的格式应该无效.

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

此刻对象中的结果日期为"Mon Apr 03 0006 ...".当我调用isValid时,它返回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.

我在代码中添加了一个regex.test以确保日期的位数正确,但是我认为在严格为真的情况下,在moment.js中应该对此进行不同的处理.

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?

推荐答案

对于过去的Moment.js版本,这是一个已知问题:

This was a known issue for past versions of Moment.js: YYYY-MM-DD with 3 digit year returns true for isValid().

该问题已在2013年12月修复,您的代码可以在该库的最新版本中正常运行.版本2.3.1 已于2013年10月发布,但不包含此修复程序

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.

这篇关于在使用严格解析时,moment.js为什么允许日期以一位数字表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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