rob
rob

Reputation: 18523

Strictly parsing dates with moment

When you create a moment from a date string and pass in the format, moment very loosely checks the date string against the format. for example the following dates are all valid

moment('1','YYYY-MM-DD').isValid() //true
moment('1988-03','YYYY-MM-DD').isValid() //true
moment('is a val1d date!?#!@#','YYYY-MM-DD').isValid() //true

Is there any way to only accept dates that match the specified format?

Upvotes: 7

Views: 3209

Answers (1)

J. Titus
J. Titus

Reputation: 9690

As of version 2.3.0, you may specify a boolean for the last argument to make Moment use strict parsing. Strict parsing requires that the format and input match exactly, including delimeters.

moment('It is 2012-05-25', 'YYYY-MM-DD').isValid();       // true
moment('It is 2012-05-25', 'YYYY-MM-DD', true).isValid(); // false

Found in this section of the Moment JS docs

Upvotes: 17

Related Questions