0

I would like to validate this date 2020-12-31T13:00:00.000Z but I couldn't find a method in moment that does the job . Kindly help

  • what do you mean validate? – pilchard Sep 17 '20 at 11:32
  • Does this answer your question? [How to parse ISO 8601 into date and time format using Moment js in Javascript?](https://stackoverflow.com/questions/39735724/how-to-parse-iso-8601-into-date-and-time-format-using-moment-js-in-javascript) – pilchard Sep 17 '20 at 11:36
  • See [\[javascript\]\[momentjs\] how to validate a date](https://stackoverflow.com/search?q=%5Bjavascript%5D%5Bmomentjs%5D+how+to+validate+a+date) and [How to validate date with Moment.js to a specified format?](https://stackoverflow.com/questions/46598667/how-to-validate-date-with-moment-js-to-a-specified-format?r=SearchResults&s=1|108.5944) – RobG Sep 17 '20 at 22:59

1 Answers1

2

Please use isValid().

    console.log(moment("2020-12-31T13:00:00.000Z").isValid());

true is returned.

    console.log(moment("2020-12-32T13:00:00.000Z").isValid());

false is returned.

kako-jun
  • 173
  • 9
  • If the intention is to validate the values and the format, the format should be supplied and strict flag should be set, otherwise *any* string that either moment.js or the built–in parser converts to a valid Date will be considered "valid", e.g. in some browsers, "foo 2020" is a valid date, not in others. – RobG Sep 17 '20 at 22:57