Use case
Today I got a question from my colleague for validating a date string in javascript.
After quick search on Google, I still don’t see have any solution for this so I write here to take note for reuse later :)
We can use moment library and standard format ISO_8601 to check
1
moment(date_str, moment.ISO_8601).isValid();
Examples
1
2
3
4
5
6
moment('2013-02-04T10:35:24-08:00', moment.ISO_8601).isValid(); // -> true
moment('2013-02-04', moment.ISO_8601).isValid(); // -> true
moment('2013-02-04 10:35', moment.ISO_8601).isValid(); // -> true
moment('2013-02-30 10:35', moment.ISO_8601).isValid(); // -> false, no 30 in Feb :)
moment('x', moment.ISO_8601).isValid(); // -> false
moment('x 1', moment.ISO_8601).isValid(); // -> false