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-working-days

v0.1.8

Published

This is a Moment.js plugin that allows you to calculate working days considering sequence of date(s) - similar to start-stop timer sequence. You can customize the week off days, and also declare custom dates for holidays (eg: public holidays) to exclude t

Downloads

705

Readme

Working days calculator - Distributed date ranges

This is a Moment.js plugin that allows you to calculate working days, considering sequence of date(s). You can customize the week off days, and also declare custom dates for holidays (eg: public holidays) to exclude them from being counted as working day(s)

Installation


npm install --save moment-working-days

Major Applications

  • Working Days calculator between Date range(s)
  • TAT (Turn Around Time Calculator)
  • Number of Fridays (or any other day) between Date range(s)

Configuration

const WorkingDays = require('moment-working-days')

const momentWorkingdays = new WorkingDays({
  includeToday: true, // optional. Default true
  verbose: true, // optional. Default false
  weekOffDays: [0, 6], // optional. Default [0, 6]
  dateFormat: 'DD-MM-YYYY', // optional. Default 'YYYY-MM-DD'
  customHolidays: ['02-12-2019'], // optional
  customWorkingDays: [] // optional: eg ['07-12-2019']
})

// includeToday: Include today in calculations, else today wll be excluded

// weekOffDays: Defines weekoff days. Note that week starts with day 0 (Sunday) to day 6 (Saturday).

// dateFormat: Moment Date Format in which dates will be passed

// customHolidays: Defines custom holidays for bussiness (eg: public holidays). Pass an array of dates in configured dateFormat

Documentation

API

getWorkingDays(array_of_dates) => number

  • calculates count of working days, considering custom holidays and weekoffs.
  • supports distributed date ranges, i.e supports multiple date ranges to be considered for calculation
  • Configure if we should consider today in calculation
  • Configure Weekoffs
  • Configure Public / custom holidays
  • Configure ustom working days
  • Support moment date formats
  • Date pairing similiar to Start-Stop timer pairing
  • If you pass only one date in array, it will calculate till today
  • If you pass range of dates, it will calculate after making pair of even and odd indices
  • Supports future dates

Example 1

> momentWorkingdays.getWorkingDays([
  "29-11-2019", "03-12-2019",
  "07-12-2019", "12-12-2019"
])

> Output
30-11-2019 is a Sat
01-12-2019 is a Sun
02-12-2019 is a Custom Holiday
07-12-2019 is a Sat
08-12-2019 is a Sun
Working Days: 6 day(s)

> Returns
6

// Explanation
- First it creates pairs of odd and even indexed dates.
- Then, it calculates days within a pair (eg: pair1: 29-11 and 03-12. pair2: 07-12 and 12-12).
- Then, it calculates overall working days.
- Hence giving support to distributed date ranges

Example 2

> momentWorkingdays.getWorkingDays([
  "29-11-2019", "03-12-2019",
  "07-12-2019"
])

> Output
30-11-2019 is a Sat
01-12-2019 is a Sun
02-12-2019 is a Custom Holiday
07-12-2019 is a Sat
08-12-2019 is a Sun
Working Days: 3 day(s)

> Returns
3

> Explanation
- If number of elements in input array is odd, then it will pair the last date with today
- Eg: pair1: 29-11 and 03-12. pair2: 07-12 and 09-12 (Todays date)
- Hence giving support to distributed date ranges

Example 3: Calculates working days from 29-11 till today

> momentWorkingdays.getWorkingDays([
  "29-11-2019",
])

> Output
30-11-2019 is a Sat
01-12-2019 is a Sun
02-12-2019 is a Custom Holiday
07-12-2019 is a Sat
08-12-2019 is a Sun
Working Days: 6 day(s)

> Returns
6

Example 4: Count all Fridays between 2 dates

> momentWorkingdays.setWeekOffDays([
  0, 1, 2, 3, 4, 6
]).getWorkingDays(["05-12-2019", "12-12-2019"]))

> Output
05-12-2019 is a Thu
07-12-2019 is a Sat
08-12-2019 is a Sun
09-12-2019 is a Mon
10-12-2019 is a Tue
11-12-2019 is a Wed
12-12-2019 is a Thu
Working Days: 1 day(s)

> Returns
1

isWorkingday(date) => boolean

  • returns if it is a working day, considering custom holidays and weekoffs

Example 1: Custom Holiday

> momentWorkingdays.isWorkingday("02-12-2019") // Monday

> Output
02-12-2019 is a Custom Holiday

> Returns
false

Example 2: WeekOff

> momentWorkingdays.isWorkingday("01-12-2019") // Sunday

> Output
01-12-2019 is a Sun

> Returns
false

Example 3: Weekday

> momentWorkingdays.isWorkingday("06-12-2019") // Friday

> Returns
true

Example 4: Custom working day

> momentWorkingdays.setCustomWorkingDays([
  '08-12-2019'
]).isWorkingday('08-12-2019')) // Sunday

> Returns
true

addWorkingDays(date, noOfDays) => Date string

  • returns date, after adding noOfDays of working days

Example

> momentWorkingdays.addWorkingDays("06-12-2019", 2) // Friday

> Output
07-12-2019 is a Sat
08-12-2019 is a Sun

> Returns
10-12-2019 // Tuesday

nextWorkingDay(date) => Date string

  • returns next working date

Example

> momentWorkingdays.nextWorkingDay("06-12-2019") // Friday

> Output
07-12-2019 is a Sat
08-12-2019 is a Sun

> Returns
09-12-2019 // Monday

subtractWorkingDays(date, noOfDays) => Date string

  • returns date, after subtracting noOfDays of working days

Example

> momentWorkingdays.subtractWorkingDays("09-12-2019", 2) // Monday

> Output
08-12-2019 is a Sun
07-12-2019 is a Sat

> Returns
05-12-2019 // Thursday

prevWorkingDay(date) => Date string

  • returns previous working day

Example

> momentWorkingdays.prevWorkingDay("09-12-2019") // Monday

> Output
08-12-2019 is a Sun
07-12-2019 is a Sat

> Returns
06-12-2019  // Friday

License

moment-working-days is copyright (c) 2019-present Yatish Balaji [email protected] and the contributors to moment-working-days.

moment-working-days is free software, licensed under the MIT License. See the LICENSE file for more details.