how to use the addbusinessdays function from date-fns in javascript

To use the addBusinessDays() function from date-fns in JavaScript, first you need to install the library via npm or yarn:

npm install date-fns
# or
yarn add date-fns
44 chars
4 lines

Then, you can import the function in your code and use it by passing in a date object and the number of business days (excluding weekends) you want to add:

index.tsx
import { addBusinessDays } from 'date-fns';

const date = new Date(2022, 0, 24); // January 24, 2022
const numBusinessDaysToAdd = 3; // add 3 business days

const newDate = addBusinessDays(date, numBusinessDaysToAdd); // January 27, 2022

console.log(newDate); // Output: Wed Jan 26 2022 00:00:00 GMT-0500 (Eastern Standard Time)
330 chars
9 lines

The addBusinessDays() function will return a new date object with the specified number of business days added to the original date object. Note that weekends (Saturday and Sunday) are not counted as business days.

gistlibby LogSnag