Peter Schura
Peter Schura

Reputation: 128

Validating dates with moment.js

Why does the following output "false"?

var date = moment(["2014", "08", "31"]);
alert( date.isValid() );

I'm using this version of the library

the same code on jsfiddle

Upvotes: 1

Views: 101

Answers (1)

Shaded
Shaded

Reputation: 17836

Months are 0 relative. So you're trying to get August 31st, 2014. In reality though, you're getting September 31st, 2014. Which is not valid because September only has 30 days.

Try this...

var date = moment(["2014", "07", "31"]);
alert( date.isValid() );

Upvotes: 2

Related Questions