]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Add quarter diff support 2131/head
authorIskren Chernev <iskren.chernev@gmail.com>
Mon, 29 Dec 2014 09:10:10 +0000 (11:10 +0200)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 29 Dec 2014 09:22:40 +0000 (11:22 +0200)
moment.js
test/moment/quarter.js

index 9057be4d83689bfc68605e8235684c7430582048..19a416929642fb56a2415192d62fffe30fb16d6d 100644 (file)
--- a/moment.js
+++ b/moment.js
 
             units = normalizeUnits(units);
 
-            if (units === 'year' || units === 'month') {
+            if (units === 'year' || units === 'month' || units === 'quarter') {
                 // average number of days in the months in the given dates
                 diff = (this.daysInMonth() + that.daysInMonth()) * 432e5; // 24 * 60 * 60 * 1000 / 2
                 // difference in months
                 daysAdjust += ((this.utcOffset() - moment(this).startOf('month').utcOffset()) -
                         (that.utcOffset() - moment(that).startOf('month').utcOffset())) * 6e4;
                 output += daysAdjust / diff;
-                if (units === 'year') {
+                if (units === 'quarter') {
+                    output = output / 3;
+                } else if (units === 'year') {
                     output = output / 12;
                 }
             } else {
index f8a2f412595d6bd6be7cf184f6d4f1e19415e7e1..cea5f84732f003ad78fdbd4b2f3d717f20ad3691 100644 (file)
@@ -119,6 +119,21 @@ exports.quarter = {
         test.done();
     },
 
+    'quarter diff' : function (test) {
+        test.equal(moment('2014-01-01').diff(moment('2014-04-01'), 'quarter'),
+                -1, 'diff -1 quarter');
+        test.equal(moment('2014-04-01').diff(moment('2014-01-01'), 'quarter'),
+                1, 'diff 1 quarter');
+        test.equal(moment('2014-05-01').diff(moment('2014-01-01'), 'quarter'),
+                1, 'diff 1 quarter');
+        test.ok(Math.abs((4 / 3) - moment('2014-05-01').diff(
+                        moment('2014-01-01'), 'quarter', true)) < 0.00001,
+                'diff 1 1/3 quarter');
+        test.equal(moment('2015-01-01').diff(moment('2014-01-01'), 'quarter'),
+                4, 'diff 4 quarters');
+        test.done();
+    },
+
     'quarter setter bubble to previous year' : function (test) {
         var m;
         test.expect(7);