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 .
use D[th]
instead: fiddle
ps I have no idea why Do
not working
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);
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.”
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.