dagda1
dagda1

Reputation: 28810

tell which whether day, month or year is invalid with momentjs

I know I can tell that a whole date is invalid with momentjs isValid:

moment(values.dateOfBirth, dateFormat).isValid()

But is there a way of telling whether it is the day, month or year part which is invalid?

e.g. if the day is 30 and the month is feb then it is the day that is invalid.

Upvotes: 1

Views: 89

Answers (1)

davidchoo12
davidchoo12

Reputation: 1321

You can do .invalidAt() to determine which date value overflowed:

var m = moment("2011-10-10T10:20:90");
m.isValid(); // false
m.invalidAt(); // 5 for seconds

The integer returned has the following meaning:

  • -1 no overflow
  • 0 years
  • 1 months
  • 2 days
  • 3 hours
  • 4 minutes
  • 5 seconds
  • 6 milliseconds

source: https://momentjs.com/docs/#/parsing/is-valid/

Upvotes: 3

Related Questions