moment-business-time
Query and manipulate moment objects within the scope of business/working hours.
Install
npm install [--save] moment-business-time
Methods
moment#isWorkingDay
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.
Example:
moment('2015-02-27').isWorkingDay();
moment('2015-02-28').isWorkingDay();
moment#isWorkingTime
Returns: Boolean
Determines if the day and time of the current instance corresponds to during business hours as defined by the currnet locale.
Example:
moment('2015-02-27T15:00:00').isWorkingTime();
moment('2015-02-27T20:00:00').isWorkingTime();
moment#nextWorkingDay
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.
Example:
moment('2015-02-28T10:00:00Z').nextWorkingDay();
moment('2015-02-28T20:00:00Z').nextWorkingDay();
moment#nextWorkingTime
Returns: moment
Returns a new moment representing the start of the next day considered to be a working day.
Example:
moment('2015-02-28T10:00:00Z').nextWorkingTime();
moment('2015-02-28T20:00:00Z').nextWorkingTime();
moment#lastWorkingDay
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.
Example:
moment('2015-02-28T10:00:00Z').lastWorkingDay();
moment('2015-02-28T20:00:00Z').lastWorkingDay();
moment#lastWorkingTime
Returns: moment
Returns a new moment representing the end of the previous day considered to be a working day.
Example:
moment('2015-02-28T10:00:00Z').lastWorkingTime();
moment('2015-02-28T20:00:00Z').lastWorkingTime();
moment#addWorkingTime
Returns: self
Adds an amount of working time to a moment, modifying the original moment instance.
Example:
moment('2015-02-27T10:00:00Z').addWorkingTime(5, 'hours');
moment('2015-02-28T10:00:00Z').addWorkingTime(5, 'hours');
moment('2015-02-27T10:00:00Z').addWorkingTime(5, 'hours', 30, 'minutes');
moment#subtractWorkingTime
Returns: self
Adds an amount of working time to a moment, modifying the original moment instance.
Example:
moment('2015-02-27T16:00:00Z').subtractWorkingTime(5, 'hours');
moment('2015-02-28T16:00:00Z').subtractWorkingTime(5, 'hours');
moment('2015-02-27T16:00:00Z').subtractWorkingTime(5, 'hours', 30, 'minutes');
moment#workingDiff
Returns: Number
Calculates the difference between two moments, counting only working time. Arguments are as per moment#diff
Example:
moment('2015-02-27T16:30:00Z').workingDiff(moment('2015-02-26T12:00:00Z'), 'hours');
moment('2015-02-27T16:30:00Z').workingDiff(moment('2015-02-26T12:00:00Z'), 'hours', true);
Configuration
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.
Example:
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
}
});
moment('Wed Feb 25 2015 15:00:00 GMT+0000').isWorkingTime()
moment('Mon Feb 23 2015 09:00:00 GMT+0000').isWorkingTime()
Running tests
npm test