From: Eric Hartford Date: Tue, 26 Nov 2013 18:50:28 +0000 (-0800) Subject: Add quarter 'q' to format string X-Git-Tag: 2.5.0^2~26^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2f5ff3a7cac593c6f9fd4f8d93debe932bfdc114;p=thirdparty%2Fmoment.git Add quarter 'q' to format string --- diff --git a/moment.js b/moment.js index 5b46691b2..6ca67035e 100644 --- a/moment.js +++ b/moment.js @@ -246,6 +246,9 @@ }, X : function () { return this.unix(); + }, + q : function () { + return this.quarterOfYear(); } }, @@ -2013,6 +2016,10 @@ return input == null ? dayOfYear : this.add("d", (input - dayOfYear)); }, + quarterOfYear : function () { + return Math.ceil((this.month() + 1.0) / 3.0); + }, + weekYear : function (input) { var year = weekOfYear(this, this.lang()._week.dow, this.lang()._week.doy).year; return input == null ? year : this.add("y", (input - year)); diff --git a/test/moment/format.js b/test/moment/format.js index c961cca8d..0a6f158c8 100644 --- a/test/moment/format.js +++ b/test/moment/format.js @@ -338,6 +338,20 @@ exports.format = { test.equal(moment.invalid().format(), "Invalid date"); test.equal(moment.invalid().format('YYYY-MM-DD'), "Invalid date"); + test.done(); + }, + + "quarter formats" : function (test) { + test.expect(7); + + test.equal(moment([1985, 1, 4]).format('q'), '1', "Feb 4 1985 is Q1"); + test.equal(moment([2029, 8, 18]).format('q'), '3', "Sep 18 2029 is Q3"); + test.equal(moment([2013, 3, 24]).format('q'), '2', "Apr 24 2013 is Q2"); + test.equal(moment([2015, 2, 5]).format('q'), '1', "Mar 5 2015 is Q1"); + test.equal(moment([1970, 0, 2]).format('q'), '1', "Jan 2 1970 is Q1"); + test.equal(moment([2001, 11, 12]).format('q'), '4', "Dec 12 2001 is Q4"); + test.equal(moment([2000, 0, 2]).format('q'), '1', "Jan 2 2000 is Q1"); + test.done(); } };