Reputation: 8792
I am using Momentjs to validate a date/time string in Javascript .
var day= "Sunday, February 14th 2010, 3:25:50 pm";
var valid=moment(day,"dddd, MMMM Do YYYY, h:mm:ss a").isValid();
alert(valid);
This always returns false . I am not sure what the issue is .
I am using the Momentjs library - http://momentjs.com/docs/#/parsing/is-valid/
I have created a jsfiddle as well - http://jsfiddle.net/FUDf7/1/
Please help .
Upvotes: 0
Views: 172
Reputation: 34823
There’s no support for ordinals in parsing—see the source code:
/************************************
Parsing
************************************/
// get the regex to find the next token
function getParseRegexForToken(token, config) {
switch (token) {
case 'DDDD':
return parseTokenThreeDigits;
case 'YYYY':
return parseTokenFourDigits;
case 'YYYYY':
return parseTokenSixDigits;
case 'S':
case 'SS':
case 'SSS':
case 'DDD':
return parseTokenOneToThreeDigits;
case 'MMM':
case 'MMMM':
case 'dd':
case 'ddd':
case 'dddd':
return parseTokenWord;
case 'a':
case 'A':
return getLangDefinition(config._l)._meridiemParse;
case 'X':
return parseTokenTimestampMs;
case 'Z':
case 'ZZ':
return parseTokenTimezone;
case 'T':
return parseTokenT;
case 'MM':
case 'DD':
case 'YY':
case 'HH':
case 'hh':
case 'mm':
case 'ss':
case 'M':
case 'D':
case 'd':
case 'H':
case 'h':
case 'm':
case 's':
return parseTokenOneOrTwoDigits;
default :
return new RegExp(token.replace('\\', ''));
}
}
It’s been reported as a bug, but it’s not going to get fixed “unless there is more demand for this.”
Upvotes: 1
Reputation: 4968
I think 14th is your problem
http://jsfiddle.net/blackjim/FUDf7/3/
var day= "Sunday, February 14 2010, 3:25:50 pm";
var valid=moment(day,"dddd MMMM D YYYY h:mm:ss a").isValid();
alert(valid);
Upvotes: 1