]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Parse quarter from string
authorAndrei Cocorean <andrei-cocorean@users.noreply.github.com>
Sun, 6 Apr 2014 17:59:33 +0000 (19:59 +0200)
committerAndrei Cocorean <andrei-cocorean@users.noreply.github.com>
Sun, 6 Apr 2014 18:08:16 +0000 (20:08 +0200)
moment.js
test/moment/create.js

index 256d96c1d316dd386b44f8bf556262c55f3738ad..2307b3700cefc123397ba81f57cb90b9fec848af 100644 (file)
--- a/moment.js
+++ b/moment.js
@@ -53,7 +53,7 @@
         isoDurationRegex = /^(-)?P(?:(?:([0-9,.]*)Y)?(?:([0-9,.]*)M)?(?:([0-9,.]*)D)?(?:T(?:([0-9,.]*)H)?(?:([0-9,.]*)M)?(?:([0-9,.]*)S)?)?|([0-9,.]*)W)$/,
 
         // format tokens
-        formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|S{1,4}|X|zz?|ZZ?|.)/g,
+        formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|Q|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|S{1,4}|X|zz?|ZZ?|.)/g,
         localFormattingTokens = /(\[[^\[]*\])|(\\)?(LT|LL?L?L?|l{1,4})/g,
 
         // parsing token regexes
     function getParseRegexForToken(token, config) {
         var a, strict = config._strict;
         switch (token) {
+        case 'Q':
+            return parseTokenOneDigit;
         case 'DDDD':
             return parseTokenThreeDigits;
         case 'YYYY':
         var a, datePartArray = config._a;
 
         switch (token) {
+        // QUARTER
+        case 'Q':
+            if (input != null) {
+                datePartArray[MONTH] = (toInt(input) - 1) * 3;
+            }
+            break;
         // MONTH
         case 'M' : // fall through to MM
         case 'MM' :
index afeb79af48ff8b2070c941803e3ebf005656d1c3..3f6beaf62879c4e0dcd2a91967f37e0de7ba5f41 100644 (file)
@@ -227,6 +227,7 @@ exports.create = {
     "string with format" : function (test) {
         moment.lang('en');
         var a = [
+                ['YYYY-Q',              '2014-4'],
                 ['MM-DD-YYYY',          '12-02-1999'],
                 ['DD-MM-YYYY',          '12-02-1999'],
                 ['DD/MM/YYYY',          '12/02/1999'],
@@ -661,6 +662,8 @@ exports.create = {
     },
 
     "strict parsing" : function (test) {
+        test.equal(moment("2014-", "YYYY-Q", true).isValid(), false, "fail missing quarter");
+
         test.equal(moment("2012-05", "YYYY-MM", true).format("YYYY-MM"), "2012-05", "parse correct string");
         test.equal(moment(" 2012-05", "YYYY-MM", true).isValid(), false, "fail on extra whitespace");
         test.equal(moment("foo 2012-05", "[foo] YYYY-MM", true).format('YYYY-MM'), "2012-05", "handle fixed text");