Question

I want to validate a date in a NodeJS application.

I tried the MomentsJs library, but it seems that the isValid() function ignored all alpha characters, it's too tolerant for me (When I specify the dateformat as YYYY-MM-DD, I expect that the date is a 10-characters-string.):

moment("One,2 and 011, 12-10", "YYYY-MM-DD").isValid()  // returns true instead of false
moment("Seppl"); // parsing seems to work with all values?

Is there any way to check dates more strictly?

How can I check if parsing a date fails? (It returns -62167222800000 when printing wrong dates it).

Is there any other better Javascript Data lib that work with different date formats and support date manipulations like MomentJs?

Était-ce utile?

La solution

Moment.js library doesnt provide any method validate the date with given format.

moment("<>", dateFormat) will just check the given date but not with given format.

Following lines from momentjs.com

Note: It is not intended to be used to validate that the input string matches the format string. Because the strictness of format matching can vary depending on the application and business requirements, this sort of validation is not included in Moment.js.

http://momentjs.com/docs/#/parsing/is-valid/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top