]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fixed duration to ms conversion
authorIskren Chernev <iskren.chernev@gmail.com>
Tue, 19 Mar 2013 06:20:41 +0000 (23:20 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Sat, 30 Mar 2013 06:18:18 +0000 (23:18 -0700)
An year was converted to 12 months and a month is 30 days, so a year was 360
days. Fixed that by taking into account full years when computing milliseconds
since unix epoch and fixing duration.asMonth to convert back a year to 12
months.

moment.js
test/moment/duration.js

index 4bf5e83690bf1e307f8daed6da922844da2c32a9..766f84fdb825db2f482b79c3d7bb06d872274295 100644 (file)
--- a/moment.js
+++ b/moment.js
         valueOf : function () {
             return this._milliseconds +
               this._days * 864e5 +
-              this._months * 2592e6;
+              (this._months % 12) * 2592e6 +
+              ~~(this._months / 12) * 31536e6;
         },
 
         humanize : function (withSuffix) {
     }
 
     makeDurationAsGetter('Weeks', 6048e5);
+    moment.duration.fn.asMonths = function () {
+        return (+this - this.years() * 31536e6) / 2592e6 + this.years() * 12;
+    };
 
 
     /************************************
index 37ac4546b5f597d12ecbc794f19d85ef941a2d4d..edf3ce86ef1f307e7a89a71f9a986f404e5ac826 100644 (file)
@@ -191,14 +191,14 @@ exports.duration = {
         });
 
         test.expect(8);
-        test.equal(Math.round(d.asYears() * 100) / 100,   2.26,        "years");
+        test.equal(Math.round(d.asYears() * 100) / 100,   2.29,        "years");
         test.equal(Math.round(d.asMonths() * 100) / 100,  27.51,       "months");
-        test.equal(Math.round(d.asWeeks() * 100) / 100,   117.91,      "weeks");
-        test.equal(Math.round(d.asDays() * 100) / 100,    825.34,      "days");
-        test.equal(Math.round(d.asHours() * 100) / 100,   19808.16,    "hours");
-        test.equal(Math.round(d.asMinutes() * 100) / 100, 1188489.33,  "minutes");
-        test.equal(Math.round(d.asSeconds() * 100) / 100, 71309360.01, "seconds");
-        test.equal(d.asMilliseconds(),                    71309360012, "milliseconds");
+        test.equal(Math.round(d.asWeeks() * 100) / 100,   119.33,      "weeks");
+        test.equal(Math.round(d.asDays() * 100) / 100,    835.34,      "days");
+        test.equal(Math.round(d.asHours() * 100) / 100,   20048.16,    "hours");
+        test.equal(Math.round(d.asMinutes() * 100) / 100, 1202889.33,  "minutes");
+        test.equal(Math.round(d.asSeconds() * 100) / 100, 72173360.01, "seconds");
+        test.equal(d.asMilliseconds(),                    72173360012, "milliseconds");
         test.done();
     },