2013-07-05 38 views
0

我正在使用Momentjs驗證JavaScript中的日期/時間字符串。問題使用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); 

這總是返回false。我不確定問題是什麼。

我使用的是Momentjs庫 - http://momentjs.com/docs/#/parsing/is-valid/

我創建了一個的jsfiddle以及 - http://jsfiddle.net/FUDf7/1/

請幫助。

+0

可能重複。 Bug?](http://stackoverflow.com/questions/16135430/baffling-behavior-when-parsing-ordinals-with-moment-js-bug) –

回答

2

使用D[th]代替:fiddle

PS我不知道爲什麼Do不工作

+0

這工程!謝謝 –

1

我覺得14號是你的問題

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); 
+0

但根據http://momentjs.com/docs/#/顯示/格式/,第14應該是一個有效的格式匹配'做' –

+0

以前沒有用過,所以我不知道它是如何工作的。嘗試做一個單獨的例子來使這個'做'工作。 – AntouanK

+0

我實際上從http://momentjs.com/docs/#/displaying/format/ –

1

有在序不支持解析 - 參見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('\\', '')); 
     } 
    } 

這是been reported as a bug,但它的not going to get fixed「除非有這更多的需求。」分析與矩JS序時[莫名其妙的行爲

+0

謝謝你的提示。 –