]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Add YYYYYY format for iso string
authorIskren Chernev <iskren.chernev@gmail.com>
Wed, 11 Dec 2013 09:18:24 +0000 (01:18 -0800)
committerIskren Chernev <iskren.chernev@gmail.com>
Wed, 11 Dec 2013 09:18:24 +0000 (01:18 -0800)
moment.js
test/moment/format.js

index 7dd6c9ab42c24c868eb1624237fc0ad74dfc1f1e..cd6be16d5093efad9b2eb040471a32144f39e223 100644 (file)
--- a/moment.js
+++ b/moment.js
@@ -39,7 +39,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]?|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]?|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
             YYYYY : function () {
                 return leftZeroFill(this.year(), 5);
             },
+            YYYYYY : function () {
+                var y = this.year(), sign = y >= 0 ? '+' : '-';
+                return sign + leftZeroFill(Math.abs(y), 6);
+            },
             gg   : function () {
                 return leftZeroFill(this.weekYear() % 100, 2);
             },
         case 'GGGG':
         case 'gggg':
             return strict ? parseTokenFourDigits : parseTokenOneToFourDigits;
+        case 'YYYYYY':
         case 'YYYYY':
         case 'GGGGG':
         case 'ggggg':
             break;
         case 'YYYY' :
         case 'YYYYY' :
+        case 'YYYYYY' :
             datePartArray[YEAR] = toInt(input);
             break;
         // AM / PM
index 64cb84ac7de97eeb15a6acef9649942272aa0e42..1207da34396cd10f09cfae746e69ffc6abf4f982 100644 (file)
@@ -166,6 +166,19 @@ exports.format = {
         test.done();
     },
 
+    "long years" : function (test) {
+        test.expect(6);
+        test.equal(moment.utc().year(2).format('YYYYYY'), '+000002', 'small year with YYYYYY');
+        test.equal(moment.utc().year(2012).format('YYYYYY'), '+002012', 'regular year with YYYYYY');
+        test.equal(moment.utc().year(20123).format('YYYYYY'), '+020123', 'big year with YYYYYY');
+
+        test.equal(moment.utc().year(-1).format('YYYYYY'), '-000001', 'small negative year with YYYYYY');
+        test.equal(moment.utc().year(-2012).format('YYYYYY'), '-002012', 'negative year with YYYYYY');
+        test.equal(moment.utc().year(-20123).format('YYYYYY'), '-020123', 'big negative year with YYYYYY');
+
+        test.done();
+    },
+
     "weeks format" : function (test) {
 
         // http://en.wikipedia.org/wiki/ISO_week_date