From: Iskren Chernev Date: Wed, 11 Dec 2013 09:18:24 +0000 (-0800) Subject: Add YYYYYY format for iso string X-Git-Tag: 2.5.0^2~10^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ea18f28cbb303e08b33ad985e3daf5d3cd1643f6;p=thirdparty%2Fmoment.git Add YYYYYY format for iso string --- diff --git a/moment.js b/moment.js index 7dd6c9ab4..cd6be16d5 100644 --- 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 @@ -172,6 +172,10 @@ 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); }, @@ -918,6 +922,7 @@ case 'GGGG': case 'gggg': return strict ? parseTokenFourDigits : parseTokenOneToFourDigits; + case 'YYYYYY': case 'YYYYY': case 'GGGGG': case 'ggggg': @@ -1030,6 +1035,7 @@ break; case 'YYYY' : case 'YYYYY' : + case 'YYYYYY' : datePartArray[YEAR] = toInt(input); break; // AM / PM diff --git a/test/moment/format.js b/test/moment/format.js index 64cb84ac7..1207da343 100644 --- a/test/moment/format.js +++ b/test/moment/format.js @@ -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