]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Using first valid format when parsing multiple formats - #653 #788
authorTim Wood <washwithcare@gmail.com>
Mon, 20 May 2013 16:31:26 +0000 (09:31 -0700)
committerTim Wood <washwithcare@gmail.com>
Mon, 20 May 2013 16:31:26 +0000 (09:31 -0700)
moment.js
test/moment/create.js
test/moment/format.js

index cf1cee9db4f6a291c23b40ba92df4668d89db9ca..09d0d5596a574ee481c7fa2c68ef9b1ceb7c93fb 100644 (file)
--- a/moment.js
+++ b/moment.js
             i,
             currentScore;
 
-        for (i = config._f.length; i > 0; i--) {
+        for (i = 0; i < config._f.length; i++) {
             tempConfig = extend({}, config);
-            tempConfig._f = config._f[i - 1];
+            tempConfig._f = config._f[i];
             makeDateFromStringAndFormat(tempConfig);
             tempMoment = new Moment(tempConfig);
 
-            if (tempMoment.isValid()) {
-                bestMoment = tempMoment;
-                break;
-            }
-
             currentScore = compareArrays(tempConfig._a, tempMoment.toArray());
 
             if (currentScore < scoreToBeat) {
index 96d058fad32a7e54d1081e05b85d182a72db84a6..a82c4e63b0bb70f86b0daa48d4a2a0c1c89b0812 100644 (file)
@@ -234,10 +234,17 @@ exports.create = {
     },
 
     "string with array of formats" : function(test) {
-        test.expect(3);
-        test.equal(moment('11-02-1999', ['MM-DD-YYYY', 'DD-MM-YYYY']).format('MM DD YYYY'), '02 11 1999', 'switching month and day');
+        test.expect(6);
+
+        test.equal(moment('11-02-1999', ['MM-DD-YYYY', 'DD-MM-YYYY']).format('MM DD YYYY'), '11 02 1999', 'switching month and day');
         test.equal(moment('02-11-1999', ['MM/DD/YYYY', 'YYYY-MM-DD', 'MM-DD-YYYY']).format('MM DD YYYY'), '02 11 1999', 'year last');
         test.equal(moment('1999-02-11', ['MM/DD/YYYY', 'YYYY-MM-DD', 'MM-DD-YYYY']).format('MM DD YYYY'), '02 11 1999', 'year first');
+
+        test.equal(moment('02-11-1999', ['MM/DD/YYYY', 'YYYY-MM-DD']).format('MM DD YYYY'), '02 11 1999', 'year last');
+        test.equal(moment('1999-02-11', ['MM/DD/YYYY', 'YYYY-MM-DD']).format('MM DD YYYY'), '02 11 1999', 'year first');
+
+        test.equal(moment('01', ["MM", "DD"])._f, "MM", "Should use first valid format");
+
         test.done();
     },
 
index b91c61378fb2a6789e9fe34a517221420cb79f00..6e84ba355b9bfef75a6ceabce8e8c9f8d38bc6d8 100644 (file)
@@ -56,7 +56,7 @@ exports.format = {
     "format multiple with zone" : function(test) {
         test.expect(1);
 
-        var b = moment('2012-10-08 -1200', ['YYYY ZZ', 'YYYY-MM-DD ZZ']);
+        var b = moment('2012-10-08 -1200', ['YYYY-MM-DD HH:mm ZZ', 'YYYY-MM-DD ZZ', 'YYYY-MM-DD']);
         test.equals(b.format('YYYY-MM'), '2012-10', 'Parsing multiple formats should not crash with different sized formats');
         test.done();
     },