Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

compare-dates

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compare-dates - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

62

dist/compare.cjs.js

@@ -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 };

2

package.json

@@ -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",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc