compare-dates
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -252,2 +252,61 @@ 'use strict'; | ||
function diff(first, second, units, asFloat) { | ||
let output; | ||
if (!isValidDate(first) || !isValidDate(second)) { | ||
throw new Error('Invalid date'); | ||
} | ||
units = normalizeUnits(units); | ||
if (units === 'year' || units === 'month' || units === 'quarter') { | ||
output = monthDiff(first, second); | ||
if (units === 'quarter') { | ||
output = output / 3; | ||
} else if (units === 'year') { | ||
output = output / 12; | ||
} | ||
} else { | ||
const delta = first - second; | ||
output = units === 'second' ? delta / 1e3 : // 1000 | ||
units === 'minute' ? delta / 6e4 : // 1000 * 60 | ||
units === 'hour' ? delta / 36e5 : // 1000 * 60 * 60 | ||
units === 'day' ? delta / 864e5 : // 1000 * 60 * 60 * 24, negate dst | ||
units === 'week' ? delta / 6048e5 : // 1000 * 60 * 60 * 24 * 7, negate dst | ||
delta; | ||
} | ||
return asFloat ? output : absFloor(output); | ||
} | ||
function absFloor(number) { | ||
if (number < 0) { | ||
// -0 -> 0 | ||
return Math.ceil(number) || 0; | ||
} else { | ||
return Math.floor(number); | ||
} | ||
} | ||
function monthDiff(a, b) { | ||
// difference in months | ||
var wholeMonthDiff = (b.getFullYear() - a.getFullYear()) * 12 + (b.getMonth() - a.getMonth()), | ||
// b is in (anchor - 1 month, anchor + 1 month) | ||
anchor = add(a, wholeMonthDiff, 'months'), | ||
anchor2, | ||
adjust; | ||
if (b - anchor < 0) { | ||
anchor2 = add(a, wholeMonthDiff - 1, 'months'); | ||
// linear across the month | ||
adjust = (b - anchor) / (anchor - anchor2); | ||
} else { | ||
anchor2 = add(a, wholeMonthDiff + 1, 'months'); | ||
// linear across the month | ||
adjust = (b - anchor) / (anchor2 - anchor); | ||
} | ||
//check for negative zero, return zero if negative zero | ||
return -(wholeMonthDiff + adjust) || 0; | ||
} | ||
exports.min = min; | ||
@@ -264,2 +323,3 @@ exports.max = max; | ||
exports.startOf = startOf; | ||
exports.endOf = endOf; | ||
exports.endOf = endOf; | ||
exports.diff = diff; |
@@ -248,2 +248,61 @@ function isValidDate (input) { | ||
export { min, max, isAfter, isBefore, isBetween, isSame, isSameOrAfter, isSameOrBefore, add, subtract, startOf, endOf }; | ||
function diff(first, second, units, asFloat) { | ||
let output; | ||
if (!isValidDate(first) || !isValidDate(second)) { | ||
throw new Error('Invalid date'); | ||
} | ||
units = normalizeUnits(units); | ||
if (units === 'year' || units === 'month' || units === 'quarter') { | ||
output = monthDiff(first, second); | ||
if (units === 'quarter') { | ||
output = output / 3; | ||
} else if (units === 'year') { | ||
output = output / 12; | ||
} | ||
} else { | ||
const delta = first - second; | ||
output = units === 'second' ? delta / 1e3 : // 1000 | ||
units === 'minute' ? delta / 6e4 : // 1000 * 60 | ||
units === 'hour' ? delta / 36e5 : // 1000 * 60 * 60 | ||
units === 'day' ? delta / 864e5 : // 1000 * 60 * 60 * 24, negate dst | ||
units === 'week' ? delta / 6048e5 : // 1000 * 60 * 60 * 24 * 7, negate dst | ||
delta; | ||
} | ||
return asFloat ? output : absFloor(output); | ||
} | ||
function absFloor(number) { | ||
if (number < 0) { | ||
// -0 -> 0 | ||
return Math.ceil(number) || 0; | ||
} else { | ||
return Math.floor(number); | ||
} | ||
} | ||
function monthDiff(a, b) { | ||
// difference in months | ||
var wholeMonthDiff = (b.getFullYear() - a.getFullYear()) * 12 + (b.getMonth() - a.getMonth()), | ||
// b is in (anchor - 1 month, anchor + 1 month) | ||
anchor = add(a, wholeMonthDiff, 'months'), | ||
anchor2, | ||
adjust; | ||
if (b - anchor < 0) { | ||
anchor2 = add(a, wholeMonthDiff - 1, 'months'); | ||
// linear across the month | ||
adjust = (b - anchor) / (anchor - anchor2); | ||
} else { | ||
anchor2 = add(a, wholeMonthDiff + 1, 'months'); | ||
// linear across the month | ||
adjust = (b - anchor) / (anchor2 - anchor); | ||
} | ||
//check for negative zero, return zero if negative zero | ||
return -(wholeMonthDiff + adjust) || 0; | ||
} | ||
export { min, max, isAfter, isBefore, isBetween, isSame, isSameOrAfter, isSameOrBefore, add, subtract, startOf, endOf, diff }; |
@@ -5,3 +5,3 @@ { | ||
"repository": "https://github.com/guardian/compare-dates", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"license": "Apache-2.0", | ||
@@ -8,0 +8,0 @@ "main": "dist/compare.cjs.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20233
544
4
2
0