0

I have this code :

var dt = moment(cdt).format("L");

At some dates moment(cdt).format("L") returns Invalid date, So how can I return an empty string instead invalid date.

  • Does this answer your question? [Momentjs : How to prevent "Invalid date"?](https://stackoverflow.com/questions/28993107/momentjs-how-to-prevent-invalid-date) – Sudhir Ojha Oct 05 '20 at 06:58
  • Does this answer your question? [Override the moment js default invalid date text](https://stackoverflow.com/questions/38953566/override-the-moment-js-default-invalid-date-text) – hgb123 Oct 05 '20 at 06:58

1 Answers1

2

With moment.js you can check if a date is valid with the method .isValid(), for instance:

moment(cdt, 'L', true).isValid()

moment("12/13/2020", "DD/MM/YYYY", true).isValid() should return False.

michaelT
  • 1,533
  • 12
  • 41
  • Note that the isValid has to be used directly on the moment-object, not on the string returned from `.format("L")`. – Sebastian Oct 05 '20 at 07:01