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

days-between-two-dates-utility

v1.0.2

Published

Identify weekdays and business days between two dates.

Downloads

15

Readme

ts-days-between-two-dates

Problem Statement

Calculate weekdays and business days between two dates.


Task One: Weekdays Between Two Dates

Calculates the number of weekdays in between two dates.

  • Weekdays are Monday, Tuesday, Wednesday, Thursday, Friday.
  • The returned count should not include either firstDate or secondDate - e.g. between Monday 07-Oct-2013 and Wednesday 09-Oct-2013 is one weekday.
  • If secondDate is equal to or before firstDate, return 0. Expected Results The following scenarios will allow you to validate your application is working as expected: |Start Date | End Date | Result |7th October 2013 | 9th October 2013 | 1 |5th October 2013 | 14th October 2013 | 5 |7th October 2013 | 1st January 2014 | 61 |7th October 2013 | 5th October 2013 | 0

Task Two: Business Days Between Two Dates

Calculate the number of business days in between two dates.

  • Business days are Monday, Tuesday, Wednesday, Thursday, Friday, but excluding any dates which appear in the supplied list of public holidays.
  • The returned count should not include either firstDate or secondDate - e.g. between Monday 07-Oct-2013 and Wednesday 09-Oct-2013 is one weekday.
  • If secondDate is equal to or before firstDate, return 0. Expected Results Sample list of Public Holidays:
  • 25th December 2013
  • 26th December 2013
  • 1st January 2014 Given those public holidays, the following scenarios will allow you to validate your application is working as expected: | Start Date | End Date | Result | 7th October 2013 | 9th October 2013 | 1 | 24th December 2013 | 27th December 2013 | 0 | 7th October 2013 | 1st January 2014 | 59

Task Three: More Holidays

Design a data structure or hierarchy of structures which can define public holidays in a more complex fashion than simple dates. This should cater for things such as:

  • Public holidays which are always on the same day, e.g. Anzac Day on April 25th every year.
  • Public holidays which are always on the same day, except when that falls on a weekend. e.g. New Year's Day on January 1st every year, unless that is a Saturday or Sunday, in which case the holiday is the next Monday.
  • Public holidays on a certain occurrence of a certain day in a month. e.g. Queen's Birthday on the second Monday in June every year. Given this data structure, the BusinessDaysBetweenTwoDates() function should be able to be extended to take a list of public holiday rules, rather than a list of DateTimes, and calculate the number of business days between two dates using those rules to define public holidays.

Solution

Class Structure

HolidayType

  • FixedHolidayType
  • AdjustedHolidayType
  • CertainDayHolidayType

PublicHolidays -> Data structure to manage all HolidayType days

Notes

  • The code separates each type of holiday into its own class, this gives a single responsibility to each class. New holiday types can be easily added.
  • The abstract base class HolidayType forces each derived class to implement the "isHoliday" function and enables different implementations.
  • There is a separation of concerns into different classes to promote readability, testability, reusability and makes it easy to maintain and evolve.
  • The PublicHolidays data structure behaves like a list which stores different types of holidays. Each holiday has a different type and specification (resources/holiday-data.json contains sample data).
  • The following have been added to make sure the code is "production-ready":
    • Unit test cases
    • Linting using ESLint
    • Code Formatting using Prettier
  • The BusinessDayCounter class has been built to be used as an NPM package.
    • npm install days-between-two-dates-utility
    • Import and instantiate BusinessDayCounter
    • Call functions with inputs
      • weekdaysBetweenTwoDates(firstDate: Date, secondDate: Date)
      • businessDaysBetweenTwoDates(firstDate: Date, secondDate: Date, publicHolidays: Date[])
      • businessDaysBetweenTwoDatesUsingDS(firstDate: Date, secondDate: Date) (requires resources/holiday-data.json for holiday types input)