From: Iskren Chernev Date: Tue, 19 Mar 2013 06:20:41 +0000 (-0700) Subject: Fixed duration to ms conversion X-Git-Tag: 2.1.0~47^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4e7d485e12e3980a38f19b598069dd9168211a59;p=thirdparty%2Fmoment.git Fixed duration to ms conversion 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. --- diff --git a/moment.js b/moment.js index 4bf5e8369..766f84fdb 100644 --- a/moment.js +++ b/moment.js @@ -1317,7 +1317,8 @@ valueOf : function () { return this._milliseconds + this._days * 864e5 + - this._months * 2592e6; + (this._months % 12) * 2592e6 + + ~~(this._months / 12) * 31536e6; }, humanize : function (withSuffix) { @@ -1354,6 +1355,9 @@ } makeDurationAsGetter('Weeks', 6048e5); + moment.duration.fn.asMonths = function () { + return (+this - this.years() * 31536e6) / 2592e6 + this.years() * 12; + }; /************************************ diff --git a/test/moment/duration.js b/test/moment/duration.js index 37ac4546b..edf3ce86e 100644 --- a/test/moment/duration.js +++ b/test/moment/duration.js @@ -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(); },