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

moment-business-time

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

moment-business-time - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

48

lib/business-hours.js

@@ -213,3 +213,51 @@ var moment = require('moment');

moment.fn.workingDiff = function workingDiff(comparator, unit, detail) {
unit = unit || 'milliseconds';
unit = moment.normalizeUnits(unit);
if (['year', 'month', 'week'].indexOf(unit) > -1) {
return this.diff(comparator, unit, detail);
}
var from, to, diff = 0, multiplier = 1;
if (this.isAfter(comparator)) {
to = this.clone();
from = comparator.clone();
multiplier = -1;
} else {
to = comparator.clone();
from = this.clone();
}
if (!from.isWorkingTime()) {
from = from.nextWorkingTime();
}
if (!to.isWorkingTime()) {
to = to.lastWorkingTime();
}
while(from.format('L') !== to.format('L')) {
if (unit === 'day') {
diff++;
} else {
diff += from.diff(openingTimes(from)[1], unit, true);
}
from = openingTimes(from.nextWorkingDay())[0];
}
if (unit === 'day') {
diff++;
} else {
diff += from.diff(to, unit, true);
}
if(!detail) {
diff = diff < 0 ? Math.ceil(diff) : Math.floor(diff);
}
return multiplier * diff;
};
module.exports = moment;

2

package.json
{
"name": "moment-business-time",
"version": "0.1.1",
"version": "0.2.0",
"description": "Query and manipulate moment objects within the context of business/working hours",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -16,3 +16,4 @@ # moment-business-time

Returns: Boolean
Returns: `Boolean`
Determines if the day of the current instance is a working day. Working days are defined as any day with working hours in the current locale.

@@ -30,3 +31,4 @@

Returns: Boolean
Returns: `Boolean`
Determines if the day and time of the current instance corresponds to during business hours as defined by the currnet locale.

@@ -44,3 +46,4 @@

Returns: moment
Returns: `moment`
Returns a new moment representing the next day considered to be a working day. The hours/minutes/seconds will be as for the source moment.

@@ -58,3 +61,4 @@

Returns: moment
Returns: `moment`
Returns a new moment representing the start of the next day considered to be a working day.

@@ -72,3 +76,4 @@

Returns: moment
Returns: `moment`
Returns a new moment representing the previous day considered to be a working day. The hours/minutes/seconds will be as for the source moment.

@@ -86,3 +91,4 @@

Returns: moment
Returns: `moment`
Returns a new moment representing the end of the previous day considered to be a working day.

@@ -100,3 +106,4 @@

Returns: self
Returns: `self`
Adds an amount of working time to a moment, modifying the original moment instance.

@@ -117,3 +124,4 @@

Returns: self
Returns: `self`
Adds an amount of working time to a moment, modifying the original moment instance.

@@ -132,5 +140,19 @@

### `moment#workingDiff`
Returns: `Number`
Calculates the difference between two moments, counting only working time. Arguments are as per [moment#diff](http://momentjs.com/docs/#/displaying/difference/)
#### Example:
```javascript
moment('2015-02-27T16:30:00Z').workingDiff(moment('2015-02-26T12:00:00Z'), 'hours');
// 12
moment('2015-02-27T16:30:00Z').workingDiff(moment('2015-02-26T12:00:00Z'), 'hours', true);
// 12.5
```
## Configuration
The working hours used for a locale can be modified using moment's `locale` method.
The working hours used for a locale can be modified using moment's `locale` method. The default working hours are 09:00-17:00, Mon-Fri.

@@ -137,0 +159,0 @@ Example:

@@ -363,2 +363,66 @@ var moment = require('../lib/business-hours');

});
describe('workingDiff', function () {
it('calculates the basic diff if the two times are on the same working day', function () {
var from = moment('2015-02-27T10:00:00'),
to = moment('2015-02-27T13:30:00');
from.workingDiff(to, 'hours').should.equal(-3);
from.workingDiff(to, 'hours', true).should.equal(-3.5);
to.workingDiff(from, 'hours', true).should.equal(3.5);
to.workingDiff(from, 'hours').should.equal(3);
from.workingDiff(to, 'minutes').should.equal(-210);
to.workingDiff(from, 'minutes').should.equal(210);
});
it('calculates the diff of only the working hours if two times are on different days', function () {
var from = moment('2015-02-27T10:00:00'),
to = moment('2015-03-02T13:30:00');
from.workingDiff(to, 'hours').should.equal(-11);
to.workingDiff(from, 'hours').should.equal(11);
from.workingDiff(to, 'hours', true).should.equal(-11.5);
to.workingDiff(from, 'hours', true).should.equal(11.5);
});
it('calculates the difference between dates in working days', function () {
var from = moment('2015-02-27T10:00:00'),
to = moment('2015-03-20T13:30:00');
from.workingDiff(to, 'days').should.equal(16);
to.workingDiff(from, 'days').should.equal(-16);
});
it('handles units that don\'t really makes sense for business opening times by deferring to moment', function () {
var from = moment('2015-02-27'),
to = moment('2015-05-27');
from.workingDiff(to, 'months').should.equal(-3);
to.workingDiff(from, 'months').should.equal(3);
});
it('handles inconsistent closing hours', function () {
moment.locale('en', {
workinghours: {
0: null,
1: ['09:30:00', '17:00:00'],
2: ['09:30:00', '17:00:00'],
3: ['09:30:00', '13:00:00'],
4: ['09:30:00', '17:00:00'],
5: ['09:30:00', '17:00:00'],
6: null
}
});
var from = moment('2015-02-23T10:00:00'),
to = moment('2015-02-26T14:00:00');
from.workingDiff(to, 'hours').should.equal(-22);
from.workingDiff(to, 'hours', true).should.equal(-22.5);
to.workingDiff(from, 'hours').should.equal(22);
to.workingDiff(from, 'hours', true).should.equal(22.5);
});
});
});
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