简体   繁体   English

使用 moment.js 的二月日期无效

[英]Invalid February date using moment.js

I'm using moment.js to format my selected date.我正在使用 moment.js 来格式化我选择的日期。 When I've selected a February date, moment.js has returned 'invalid date':当我选择了 2 月的日期时,moment.js 返回了“无效日期”:

moment('28/02/2018').isValid(); //returns false

But, when I've selected a March date, moment.js has returned a valid date:但是,当我选择了三月日期时,moment.js 返回了一个有效日期:

moment('01/03/2018').isValid(); //returns true

Can someone say me what I'm doing wrong?有人可以告诉我我做错了什么吗?

You're passing in a date in the format of DD/MM/YYYY , you'll have to describe the format when parsing:您以DD/MM/YYYY格式传入日期,解析时必须描述格式:

moment('28/02/2018', 'DD/MM/YYYY').isValid(); // returns true

Note: The second example works, but is not a March date, it's January 3rd.注意:第二个示例有效,但不是 3 月的日期,而是 1 月 3 日。 That should be a clue as to why the first example doesn't work.这应该是第一个示例为什么不起作用的线索。

When you provide an input that is not recognizable, moment will throw a warning in your console .当您提供无法识别的输入时, moment 会在您的控制台中发出警告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM