简体   繁体   中英

Invalid February date using moment.js

I'm using moment.js to format my selected date. When I've selected a February date, moment.js has returned 'invalid date':

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

But, when I've selected a March date, moment.js has returned a valid date:

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:

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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