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

moment-business-days

v1.2.0

Published

MomentJS plugin to use business days

Downloads

596,890

Readme

moment-business-days

This is a Moment.js plugin that allows you to work with only business days (Monday to Friday). You can customize the working week, and also set custom dates for holidays to exclude them from being counted as business days, for example national holidays.

Notes

  • This plugin works on both server and client side.
  • This plugin is based on momentjs-business.
  • All contributions are welcome.
  • Thanks to the contributors for making this plugin better!!

Usage

// NodeJS: require instead of standard moment package
var moment = require('moment-business-days');
// You'll be able use Moment.js as you normally do
<!-- Browser -->
<!-- NB: add after moment.js -->
<script src="moment.js"></script>
<script src="moment-business-days.js"></script>

Configuration

Use localization to configure holidays

var moment = require('moment-business-days');

var july4th = '07-04-2015';
var laborDay = '09-07-2015';

moment.updateLocale('us', {
   holidays: [july4th, laborDay],
   holidayFormat: 'MM-DD-YYYY'
});

// moment-business-days will now stop considering these holidays as business days

Use localization to configure business days

var moment = require('moment-business-days');

moment.updateLocale('us', {
   workingWeekdays: [1, 2, 3, 4, 5, 6]
});

// Defines days from 1 (Monday) to 6 (Saturday) as business days. Note that Sunday is day 0.
// When omitting this configuration parameter, business days are based on locale default

API

The objects returned by methods are Moment.js objects (except .isBusinessDay() and .businessDiff()) so you can handle them with Moment.js native methods.

.isHoliday() => boolean

Check if the date is among the holidays specified, and return true or false:

.isBusinessDay() => boolean

Check if the date is a business day, and return true or false:

// 31-01-2015 is Saturday
moment('31-01-2015', 'DD-MM-YYYY').isBusinessDay() // false

// 30-01-2015 is Friday
moment('30-01-2015', 'DD-MM-YYYY').isBusinessDay() // true

.businessDaysIntoMonth() => number

Calculate the amount of business days in the month of the Moment.js object.

.businessDiff() => number

Calculate the amount of business days between dates.

var diff = moment('05-15-2017', 'MM-DD-YYYY').businessDiff(moment('05-08-2017','MM-DD-YYYY'));
// diff = 5

Note that the default behavior of businessDiff is to return an absolute value, which is a departure from moment's diff. To match the behavior of diff pass true as the second argument to businessDiff:

var diff = moment('05-08-2017', 'MM-DD-YYYY').businessDiff(moment('05-15-2017','MM-DD-YYYY'), true);
// diff = -5

.businessAdd(days) => Moment

Will add the given number of days skipping non-business days, returning a Moment.js object:

// 30-01-2015 is Friday, DD-MM-YYYY is the format
moment('30-01-2015', 'DD-MM-YYYY').businessAdd(3)._d // Wed Feb 04 2015 00:00:00 GMT-0600 (CST)

.businessSubtract(days) => Moment

Will subtract the given number of days skipping non-business days, returning a Moment.js object:

// 27-01-2015 is Tuesday, DD-MM-YYYY is the format
moment('27-01-2015', 'DD-MM-YYYY').businessSubtract(3)._d // Thu Jan 22 2015 00:00:00 GMT-0600 (CST)

.nextBusinessDay() => Moment

Will retrieve the next business date as a Moment.js object:

// Next business day from Friday 30-01-2015
moment('30-01-2015', 'DD-MM-YYYY').nextBusinessDay()._d // Mon Feb 02 2015 00:00:00 GMT-0600 (CST)

// Next business day from Monday 02-02-2015
moment('02-02-2015', 'DD-MM-YYYY').nextBusinessDay()._d //Tue Feb 03 2015 00:00:00 GMT-0600 (CST)

By default only 7 days into the future are checked for the next business day. To search beyond 7 days set the nextBusinessDayLimit (as a number) higher.

var moment = require('moment-business-days');

moment.updateLocale('us', {
   nextBusinessDayLimit: 31
});

.prevBusinessDay() => Moment

Will retrieve the previous business date as a Moment.js object:

// Previous business day of Monday 02-02-2015
moment('02-02-2015', 'DD-MM-YYYY').prevBusinessDay()._d // Fri Jan 30 2015 00:00:00 GMT-0600 (CST)

// Previous business day of Tuesday 03-02-2015
moment('03-02-2015', 'DD-MM-YYYY').prevBusinessDay()._d //Mon Feb 02 2015 00:00:00 GMT-0600 (CST)

By default only the last 7 days are checked for the previous business day. To search beyond 7 days set the prevBusinessDayLimit (as a number) higher.

var moment = require('moment-business-days');

moment.updateLocale('us', {
   prevBusinessDayLimit: 31
});

.monthBusinessDays() => Moment[]

Retrieve an array of the business days in the month, each one is a Moment.js object.

// Business days in month January 2015
moment('01-01-2015', 'DD-MM-YYYY').monthBusinessDays()

/*
[ { _isAMomentObject: true,
    _i: '01-01-2015',
    _f: 'DD-MM-YYYY',
    _isUTC: false,
    _pf:{ ... },
    _locale: { ... },
    _d: Thu Jan 01 2015 00:00:00 GMT-0600 (CST)
  } {
   ...
  },
  ( ... )
]
*/

.monthNaturalDays() => Moment[]

Is like .monthBusinessDays(), but this method will include the weekends in it's response.

.monthBusinessWeeks() => Moment[][]

Retrieve an array of arrays, these arrays are the representation of a business weeks and each week (array) have it own business days (Monday to Friday). There could be the case that one week (array) have less than 5 days, this is because the month started in the middle of a week, for example: the first week of January 2015 has just two days, Thursday 1st and Friday 2nd. Each day in the week arrays are Moment.js objects.

// Business weeks in month January 2015
moment('01-01-2015', 'DD-MM-YYYY').monthBusinessWeeks()

/*
[ [ { _isAMomentObject: true,
      _i: '01-01-2015',
      _f: 'DD-MM-YYYY',
      _isUTC: false,
      _pf: [...],
      _locale: [...],
      _d: Thu Jan 01 2015 00:00:00 GMT-0600 (CST)
    }, { _isAMomentObject: true,
      _i: '01-01-2015',
      _f: 'DD-MM-YYYY',
      _isUTC: false,
      _pf: [...],
      _locale: [...],
      _d: Fri Jan 02 2015 00:00:00 GMT-0600 (CST) }
  ],
  [...]
]
*/

.monthNaturalWeeks() => Moment[][]

It's like .monthBusinessWeeks(), but this method will include weekends in it's response.

Installation

// For Node.js
$ npm install moment-business-days

// ...or install and save in package.json
$ npm install --save moment-business-days

// For bower
$ bower install moment-business-days

Testing

npm test