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

To use the previousMonday function from the date-fns library in JavaScript, you first need to install the date-fns package using npm. To do this, run the following command in your terminal:

index.tsx
npm install date-fns
21 chars
2 lines

Once you have installed the package, you can use the previousMonday function as follows:

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

const date = new Date();
const lastMonday = previousMonday(date);

console.log(lastMonday); // Output: Mon Aug 02 2021 00:00:00 GMT-0400 (Eastern Daylight Time)
210 chars
7 lines

In this example, we have imported the previousMonday function from the date-fns library using destructuring. We then create a new Date object and pass it to the previousMonday function to get the date of the previous Monday.

The previousMonday function returns a new Date object representing the previous Monday relative to the input date. If the input date is already a Monday, the same date is returned.

Hope this helps!

gistlibby LogSnag