npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serious-business-time

v0.3.0

Published

rewrite of moment-business-time

Downloads

186

Readme

serious-business-time

Query and manipulate moment objects within the scope of business/working hours. A re-organisation of moment-business-time such that global scope access to business hours etc is removed.

WARNING: The docs maybe a little out of date!

In general, the library has been modified such that it no longer mutates/monkey-patches moment and instead operates on moment instances instead.

Install

npm install [--save] serious-business-time

Usage

var SeriousBusinessTime = require('serious-business-time');
var moment = require('moment');

var workinghours = {
    0: null,
    1: ['09:00:00', '17:00:00'],
    2: ['09:00:00', '17:00:00'],
    3: ['09:00:00', '17:00:00'],
    4: ['09:00:00', '17:00:00'],
    5: ['09:00:00', '17:00:00'],
    6: null
}

// optional, if not specified defaults to empty array
var holidays = [];

var myBusinessTime = SeriousBusinessTime.createInstance(moment, workinghours, holidays);

Methods

SeriousBusinessTime#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:

myBusinessTime.isWorkingDay(moment('2015-02-27'));
// true
myBusinessTime.isWorkingDay(moment('2015-02-28'));
// false

SeriousBusinessTime#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:

myBusinessTime.isWorkingTime(moment('2015-02-27T15:00:00');
// true
myBusinessTime.isWorkingTime(moment('2015-02-27T20:00:00'));
// false

SeriousBusinessTime#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:

myBusinessTime.nextWorkingDay((moment('2015-02-28T10:00:00Z'));
// Mon Mar 02 2015 10:00:00 GMT+0000
myBusinessTime.nextWorkingDay(moment('2015-02-28T20:00:00Z'));
// Mon Mar 02 2015 20:00:00 GMT+0000

SeriousBusinessTime#nextWorkingTime

Returns: moment

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

Example:

myBusinessTime.nextWorkingTime((moment('2015-02-28T10:00:00Z'));
// Mon Mar 02 2015 09:00:00 GMT+0000
myBusinessTime.nextWorkingTime(moment('2015-02-28T20:00:00Z'));
// Mon Mar 02 2015 09:00:00 GMT+0000

SeriousBusinessTime#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:

myBusinessTime.lastWorkingDay(moment('2015-02-28T10:00:00Z'));
// Fri Feb 27 2015 10:00:00 GMT+0000
myBusinessTime.lastWorkingDay(moment('2015-02-28T20:00:00Z'));
// Fri Feb 27 2015 20:00:00 GMT+0000

SeriousBusinessTime#lastWorkingTime

Returns: moment

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

Example:

myBusinessTime.lastWorkingTime(moment('2015-02-28T10:00:00Z'));
// Fri Feb 27 2015 17:00:00 GMT+0000
myBusinessTime.lastWorkingTime(moment('2015-02-28T20:00:00Z'));
// Fri Feb 27 2015 17:00:00 GMT+0000

SeriousBusinessTime#addWorkingTime

Returns: self

Adds an amount of working time to a moment, modifying the original moment instance.

Example:

myBusinessTime.addWorkingTime(moment('2015-02-27T10:00:00Z'), 5, 'hours');
// Fri Feb 27 2015 15:00:00 GMT+0000
myBusinessTime.addWorkingTime(moment('2015-02-28T10:00:00Z'), 5, 'hours');
// Mon Mar 02 2015 14:00:00 GMT+0000
myBusinessTime.addWorkingTime(moment('2015-02-27T10:00:00Z'), 30, 'minutes');
// Fri Feb 27 2015 10:30:00 GMT+0000

SeriousBusinessTime#subtractWorkingTime

Returns: self

Adds an amount of working time to a moment, modifying the original moment instance.

Example:

myBusinessTime.subtractWorkingTime(moment('2015-02-27T16:00:00Z'), 5, 'hours');
// Fri Feb 27 2015 11:00:00 GMT+0000
myBusinessTime.subtractWorkingTime(moment('2015-02-28T16:00:00Z'), 5, 'hours');
// Fri Feb 27 2015 12:00:00 GMT+0000
myBusinessTime.subtractWorkingTime(moment('2015-02-27T16:00:00Z'), 30, 'minutes');
// Fri Feb 27 2015 15:30:00 GMT+0000

SeriousBusinessTime#workingDiff

Returns: Number

Calculates the difference between two moments, counting only working time. Arguments are as per moment#diff

Example:

myBusinessTime.workingDiff(moment('2015-02-27T16:30:00Z'), moment('2015-02-26T12:00:00Z'), 'hours');
// 12
myBusinessTime.workingDiff(moment('2015-02-27T16:30:00Z'), moment('2015-02-26T12:00:00Z'), 'hours', true);
// 12.5

Configuration

Holidays

Holidays which should not be considered as working days can be configured by passing them as array as the third parameter.

Example:

var holidays = [
        '2015-05-04'
    ];

var myBizTime = SeriousBusinessTime.createInstance(moment, workinghours, holidays)

myBizTime.isWorkingDay(moment('2015-05-04')) // false

Recurring holidays can also be set with wildcard parameters.

var holidays: ['*-12-25'];

var myBizTime = SeriousBusinessTime.createInstance(moment, workinghours, holidays)

myBizTime.isWorkingDay(moment('2015-12-25')) // false
myBizTime.isWorkingDay(moment('2016-12-25')) // false
myBizTime.isWorkingDay(moment('2017-12-25')) // false
myBizTime.isWorkingDay(moment('2018-12-25')) // false

Running tests

npm test