]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Add quarter 'q' to format string
authorEric Hartford <ericstob@gmail.com>
Tue, 26 Nov 2013 18:50:28 +0000 (10:50 -0800)
committerEric Hartford <ericstob@gmail.com>
Tue, 26 Nov 2013 18:50:28 +0000 (10:50 -0800)
moment.js
test/moment/format.js

index 5b46691b2edb47b5eaf6e8b9252135915feccf57..6ca67035eb8d7a7767a8f2b9a72ebc509685a93d 100644 (file)
--- a/moment.js
+++ b/moment.js
             },
             X    : function () {
                 return this.unix();
+            },
+            q : function () {
+                return this.quarterOfYear();
             }
         },
 
             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));
index c961cca8d135e2168ca7f1104328f4d1062981fd..0a6f158c817f19f5ebc28dd4e83320c6e078958f 100644 (file)
@@ -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();
     }
 };