Giter Site home page Giter Site logo

soar-tech / dayjs-business-days Goto Github PK

View Code? Open in Web Editor NEW
32.0 2.0 10.0 490 KB

Day.js plugin to add support for calculating dates only accounting for Business days

License: MIT License

JavaScript 100.00%
dayjs business-days working-days plugin dayjs-plugin dayjs-business-days

dayjs-business-days's Introduction

dayjs-business-days

This is a plugin for Day.js that allows for Date calculations to take place that only consider Business Days i.e Monday to Friday

  • Calculate if date is a Business Day
  • Add/Subtract Business Days
  • Calculate difference between dates in Business Days
  • Get Next/Prev Business Day
  • Get all Business Days/Weeks in a Month
  • Based off of moment-business-days

Current CI/CD Status

CI not yet implemented

Contents

Getting Started

The following guide will help you use the plugin with Day.js, and explain the plugins API.

Prerequisites

Day.js version 1.8.x installed

Installing

You can install via Yarn or npm

yarn add dayjs-business-days
npm install dayjs-business-days

Usage Guide

You will need to import the plugin and activate it via the Day.js .extend() function

import dayjs from 'dayjs';
import dayjsBusinessDays from 'dayjs-business-days';

dayjs.extend(dayjsBusinessDays);

isBusinessDay() => Boolean

Check if the date is a business day. Returns true or false

// Christmas day is a Friday
dayjs('2020-12-25').isBusinessDay(); // returns true

// Boxing day is a Saturday
dayjs('2020-12-26').isBusinessDay(); // returns false

businessDaysAdd(number) => Day.js Object

  • number {Number} Number of Business Days to be added

Adds the number of Business Days to the current date. Returns the new date as a Day.js object

dayjs('2020-12-25').businessDaysAdd(3).format('DD/MM/YYYY'); // returns 30/12/2020

businessDaysSubtract(number) => Day.js Object

  • number {Number} Number of Business Days to be subtracted

Subtracts the number of Business Days from the current date. Returns the new date as a Day.js object

dayjs('2020-12-30').businessDaysSubtract(3).format('DD/MM/YYYY'); // returns 25/12/2020

businessDiff(date) => Number

  • date {Day.js Date} The date to be diffed from

Calculates the number of business days between a Day.js date and date. Returns the difference as a positive or negative number.

dayjs('2020-12-25').businessDiff(dayjs('2020-12-30')); // returns -5
dayjs('2020-12-30').businessDiff(dayjs('2020-12-25')); // returns 5

nextBusinessDay() => Day.js Object

Calculates the next Business Day. Returns a Day.js object

// 25th December 2020 is a Friday. Next business day is Monday 28th December.
dayjs('2020-12-25').nextBusinessDay().format('DD/MM/YYYY'); // returns 28/12/2020

prevBusinessDay() => Day.js Object

Calculates the previous Business Day. Returns a Day.js object

// 28th December 2020 is a Monday. Previous business day is Friday 25th December.
dayjs('2020-12-28').prevBusinessDay().format('DD/MM/YYYY'); // returns 25/12/2020

businessDaysInMonth() => [Day.js Object]

Calculates all of the business days in a given month. Returns an array of Day.js objects

dayjs('2020-12-25').businessDaysInMonth();
// returns equivalent of [dayjs('2020-12-01'), dayjs('2020-12-02'), ...]

businessWeeksInMonth() => [[Day.js Object]]

Calculates all of the business weeks and days in a given month. Returns an two dimensional array of Day.js objects

dayjs('2020-12-25').businessWeeksInMonth();
// returns equivalent of
// [
//   [dayjs('2020-12-01'), dayjs('2020-12-02'), ...],
//   [dayjs('2020-12-07'), dayjs('2020-12-08'), ...],
//   ...
// ]

isHoliday() => Boolean

Check if the date is a holiday. Returns true or false

// Add holidays to plugin options
const options = {
  holidays: ['2020-12-25'],
  holidayFormat: 'YYYY-MM-DD',
};
dayjs.extend(businessDays, options);

// Christmas day is a Friday
dayjs('2020-12-25').isHoliday(); // returns true

Local Development and Contributing

We are more than happy to accept PRs for bugs, improvements or new features. Developing your own changes locally is easy, you just need to clone the repo

git clone [email protected]/soar-tech/dayjs-business-days

cd dayjs-business-days

and install the dependencies with either npm or yarn

npm i
yarn

Tests can be ran with the test script

npm run test
yarn test

dayjs-business-days's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

dayjs-business-days's Issues

Infinite loop when adding to invalid dates.

When doing the following, I get an infinite loop:

const x = dayjs(null).businessDaysAdd(1);

This is probably due to the while loop in the businessDaysAdd method. It proceeds to the next day, reduces the days to add by one if that next day is a business day, and repeats until days to add reach zero.
When the starting date was invalid, no day will ever be a business day and the remaining days will never decrease.

My advice would be changing line 23 in index.js from

while (daysRemaining > 0) {

to something like

while (currentDay.isValid() && daysRemaining > 0) {

plus adding a test.

crash when calling businessTimeDiff with no business day exists between startdate and enddate

var dayjs = require('dayjs');
var dayjsBusinessTime = require("dayjs-business-time")
dayjs.extend( dayjsBusinessTime);
    const businessTimes = {
      sunday: null,
      monday: [
        { start: '07:00:00', end: '19:00:00' }
      ],
      tuesday: [
        { start: '07:00:00', end: '19:00:00' }
      ],
      wednesday: [
        { start: '07:00:00', end: '19:00:00' }
      ],
      thursday: [
        { start: '09:00:00', end: '19:00:00' }
      ],
      friday: [
        { start: '07:00:00', end: '19:00:00' }
      ],
      saturday: null,
    }
    dayjs.setBusinessTime(businessTimes);
    
    let start = dayjs('2021-03-05 20:07:10'); // next business day would be after 'end'
    let end  = dayjs('2021-03-08 10:07:10');
    let difference = 0; 
    
   
    // if( ! end.isBefore(start.nextBusinessDay())) { //workaround

        difference = start.businessTimeDiff(end, 'days');   // <--- crash

    //}
       
    console.log( 'Difference: ' + difference )

Thank you for your great work!

Bug, holiday is not available

Hello, in the latest version of release, the code with holiday is not present.

So after the install by yarn, i wont be able to use isHolliday() function.

Can you generate new release please ? ๐Ÿ™

Contribution - custom workingweekdays

Hi,

I'd like to open a PR so that one can set custom workingWeekdays. It seems I need to be allowed to open PRs, is there anything I should do in order to do so?

Requiring with CJS Module Format doesn't behave as expected

Recently I created my own dayjs plugin, dayjs-random largely using this plugin as a template.

While doing so I noticed that when importing this plugin using the cjs module format to be used on my server didn't quite work as expected.

const dayjs = require('dayjs')
const dayjsBusinessDays = require('dayjs-business-days')

dayjs.extend(dayjsBusinessDays)

This code results in the following error

....

TypeError: t is not a function
    at Function.g.extend (C:\$Projects\dayjs-random-test\server\node_modules\dayjs\dayjs.min.js:1:6481)
    at Object.<anonymous> (C:\$Projects\dayjs-random-test\server\index.js:4:7)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

In order to import it properly I had to add .default to the end of the require

const dayjs = require('dayjs')
const dayjsBusinessDays = require('dayjs-business-days').default

dayjs.extend(dayjsBusinessDays)

Use custom holidays

Is there an opportunity to use custom holidays?

Like it is in moment-business-days updateLocale

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.