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

date-arithmetic

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

date-arithmetic - npm Package Compare versions

Comparing version 3.0.1 to 3.1.0

84

index.js

@@ -27,3 +27,3 @@ var MILI = 'milliseconds'

case WEEK:
return dates.date(date, dates.date(date) + (7 * num))
return dates.date(date, dates.date(date) + (7 * num))
case MONTH:

@@ -65,9 +65,9 @@ return monthMath(date, num)

if (unit === DECADE)
if (unit === DECADE)
date = dates.subtract(date, dates.year(date) % 10, 'year')
if (unit === CENTURY)
if (unit === CENTURY)
date = dates.subtract(date, dates.year(date) % 100, 'year')
if (unit === WEEK)
if (unit === WEEK)
date = dates.weekday(date, 0, firstOfWeek);

@@ -78,2 +78,3 @@

endOf: function(date, unit, firstOfWeek){

@@ -101,3 +102,3 @@ date = new Date(date)

},
inRange: function(day, min, max, unit){

@@ -120,3 +121,3 @@ unit = unit || 'day'

decade: function (date, val) {
return val === undefined
return val === undefined
? dates.year(dates.startOf(date, DECADE))

@@ -127,3 +128,3 @@ : dates.add(date, val + 10, YEAR);

century: function (date, val) {
return val === undefined
return val === undefined
? dates.year(dates.startOf(date, CENTURY))

@@ -136,9 +137,62 @@ : dates.add(date, val + 100, YEAR);

return val === undefined
? weekday
return val === undefined
? weekday
: dates.add(date, val - weekday, DAY);
},
diff: function (date1, date2, unit, asFloat) {
var dividend, divisor, result;
switch (unit) {
case MILI:
case SECONDS:
case MINUTES:
case HOURS:
case DAY:
case WEEK:
dividend = date2.getTime() - date1.getTime(); break;
case MONTH:
case YEAR:
case DECADE:
case CENTURY:
dividend = (dates.year(date2) - dates.year(date1)) * 12 + dates.month(date2) - dates.month(date1); break;
default:
throw new TypeError('Invalid units: "' + unit + '"');
}
switch (unit) {
case MILI:
divisor = 1; break;
case SECONDS:
divisor = 1000; break;
case MINUTES:
divisor = 1000 * 60; break;
case HOURS:
divisor = 1000 * 60 * 60; break;
case DAY:
divisor = 1000 * 60 * 60 * 24; break;
case WEEK:
divisor = 1000 * 60 * 60 * 24 * 7; break;
case MONTH:
divisor = 1; break;
case YEAR:
divisor = 12; break;
case DECADE:
divisor = 120; break;
case CENTURY:
divisor = 1200; break;
default:
throw new TypeError('Invalid units: "' + unit + '"');
}
result = dividend / divisor;
return asFloat ? result : absoluteFloor(result);
}
};
function absoluteFloor(number) {
return number < 0 ? Math.ceil(number) : Math.floor(number);
}
function monthMath(date, val){

@@ -150,4 +204,4 @@ var current = dates.month(date)

if (newMonth < 0 ) newMonth = 12 + val
while (newMonth < 0 ) newMonth = 12 + newMonth
//month rollover

@@ -172,5 +226,5 @@ if ( dates.month(date) !== ( newMonth % 12))

function createComparer(operator) {
return function (a, b, unit, maybeFoW) {
return operator(+dates.startOf(a, unit, maybeFoW), +dates.startOf(b, unit, maybeFoW))
return function (a, b, unit) {
return operator(+dates.startOf(a, unit), +dates.startOf(b, unit))
};
}

@@ -0,0 +0,0 @@ The MIT License (MIT)

7

package.json
{
"name": "date-arithmetic",
"version": "3.0.1",
"description": "simple immutable date math util",
"version": "3.1.0",
"description": "simple date math util",
"main": "index.js",

@@ -16,5 +16,2 @@ "scripts": {

"date",
"datetime",
"immutable",
"locale",
"math"

@@ -21,0 +18,0 @@ ],

@@ -33,3 +33,3 @@

return a new date with the relevent date parts zero'd out.
return a new date with the relevent date parts zero'd out. You only need to provide a `firstOfWeek` when the unit is `'week'`

@@ -64,3 +64,4 @@ dateMath.startOf(new Date, 'day') // -> no time components

- `dateMath.max(dateA, dateB, dateN)`
- `dateMath.diff(dateA, dateB, unit, asFloat)`
Valid unit values are; `"second", "minutes", "hours", "day", "week", "month", "year", "decade", "century" `
Valid unit values are; `"second", "minutes", "hours", "day", "week", "month", "year", "decade", "century" `
var assert = require('assert')
, dateMath = require('./index')
var date = new Date(
2014 /* year */
, 1 /* month */
, 18 /* day */
, 8 /* hour */
, 25 /* min */
, 30 /* sec */
, 5); /* ms */
var date = new Date(
2014 /* year */
, 1 /* month */
, 18 /* day */
, 8 /* hour */
, 25 /* min */
, 30 /* sec */
, 5); /* ms */

@@ -24,3 +24,3 @@ console.log('---- Accessors ----------------------------')

console.log(' past')
console.log(' passed')
console.log('---- start of ----------------------------')

@@ -36,3 +36,3 @@

console.log(' past')
console.log(' passed')
console.log('---- Date Math ----------------------------')

@@ -50,2 +50,3 @@

assert.equal(+dateMath.add(date, 1, 'milliseconds'), +(new Date(2014, 1, 18, 8, 25, 30, 6)), 'add milliseconds')
assert.equal(+dateMath.subtract(date, 24, 'month'), +dateMath.subtract(date, 2, 'year'), 'month rollover')

@@ -70,2 +71,31 @@ assert.equal(+dateMath.max(date, new Date(2013, 0, 1, 0, 0, 0, 0)), +date, 'max')

console.log(' past')
assert.equal(dateMath.diff(date, date, 'milliseconds'), 0)
assert.equal(dateMath.diff(dateMath.subtract(date, 100, 'milliseconds'), date, 'milliseconds'), 100)
assert.equal(dateMath.diff(date, dateMath.subtract(date, 100, 'milliseconds'), 'milliseconds'), -100)
assert.equal(dateMath.diff(dateMath.subtract(date, 100, 'milliseconds'), date, 'seconds'), 0)
assert.equal(dateMath.diff(date, dateMath.subtract(date, 100, 'milliseconds'), 'seconds'), 0)
assert.equal(dateMath.diff(dateMath.subtract(date, 100, 'milliseconds'), date, 'seconds', true), 0.1)
assert.equal(dateMath.diff(date, dateMath.subtract(date, 100, 'milliseconds'), 'seconds', true), -0.1)
assert.equal(dateMath.diff(dateMath.subtract(date, 1000, 'milliseconds'), date, 'seconds'), 1)
assert.equal(dateMath.diff(dateMath.subtract(date, 12, 'minutes'), date, 'minutes'), 12)
assert.equal(dateMath.diff(dateMath.subtract(date, 2, 'hours'), date, 'minutes'), 120)
assert.equal(dateMath.diff(dateMath.subtract(date, 2, 'hours'), date, 'hours'), 2)
assert.equal(dateMath.diff(dateMath.subtract(date, 1, 'day'), date, 'day'), 1)
assert.equal(dateMath.diff(dateMath.subtract(date, 125, 'month'), date, 'month'), 125)
assert.equal(dateMath.diff(dateMath.subtract(date, 125, 'month'), date, 'year'), 10)
assert.equal(dateMath.diff(date, dateMath.subtract(date, 125, 'month'), 'year'), -10)
assert.equal(dateMath.diff(dateMath.subtract(date, 126, 'month'), date, 'year', true), 10.5)
assert.equal(dateMath.diff(date, dateMath.subtract(date, 126, 'month'), 'year', true), -10.5)
assert.equal(dateMath.diff(dateMath.subtract(date, 125, 'month'), date, 'decade'), 1)
assert.equal(dateMath.diff(dateMath.subtract(date, 250, 'month'), date, 'decade'), 2)
assert.equal(dateMath.diff(dateMath.subtract(date, 10, 'year'), date, 'century'), 0)
assert.equal(dateMath.diff(dateMath.subtract(date, 100, 'year'), date, 'century'), 1)
assert.equal(dateMath.diff(dateMath.subtract(date, 101, 'year'), date, 'century'), 1)
assert.equal(dateMath.diff(date, dateMath.subtract(date, 101, 'year'), 'century'), -1)
assert.equal(dateMath.diff(dateMath.subtract(date, 101, 'year'), date, 'century', true), 1.01)
assert.equal(dateMath.diff(dateMath.subtract(date, 201, 'year'), date, 'century'), 2)
assert.throws(function () {
dateMath.diff(dateMath.subtract(date, 201, 'year'), date, 'unknown');
}, /Invalid units: "unknown"/);
console.log(' passed')

Sorry, the diff of this file is not supported yet

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