Moment.js 日期验证

Moment.js Date Validation

这里是从 chrome 控制台获取的一些代码。看起来 moment.js 并没有强制 'year' 参数为 4 个字符,即使那是我在格式中声明的。

date
"16/01/14"
moment( date, 'YYYY-MM-DD').isValid()
true

总之试试

moment( "16/01/14", 'YYYY-MM-DD').isValid()

我以为这是假的,但这是真的。无论如何强制时刻更严格?

来自 http://momentjs.com/docs/#/parsing/ :

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( "16/01/14", 'YYYY-MM-DD', true).isValid()
> false