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

You can use the nextFriday function from the date-fns library to get the next Friday date from a given date.

Here's an example code snippet:

index.tsx
const { nextFriday } = require('date-fns');

const date = new Date('2022-07-01'); // create a new date object
const nextFridayDate = nextFriday(date); // get the next Friday date from the given date

console.log(nextFridayDate); // Fri Jul 08 2022 00:00:00 GMT+0530 (India Standard Time)
288 chars
7 lines

In the above code:

  • We first import the nextFriday function from date-fns using the destructuring syntax.
  • We then create a new Date object with the date we want to find the next Friday from.
  • Finally, we call the nextFriday function with the created Date object and store the result in nextFridayDate. This returns the next Friday date from the given date.
  • We then log the nextFridayDate to the console.

Note: The nextFriday function returns a Date object with the time set to 00:00:00 hours.

gistlibby LogSnag