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

To use the previousSunday function from date-fns in JavaScript, you first need to install the date-fns library by running the following command in your terminal:

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

Then, you can import and use the previousSunday function like this:

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

const today = new Date();
const previousSundayDate = previousSunday(today);

console.log(previousSundayDate);
// Output: Sun May 23 2021 00:00:00 GMT+0530 (India Standard Time)
226 chars
8 lines

The previousSunday function takes one argument – the date for which you want to find the previous Sunday. It returns a Date object representing the previous Sunday.

In the above code snippet, we are getting today's date using the new Date() constructor and passing it as an argument to the previousSunday function. This will return the date for the previous Sunday.

Finally, we are logging the output to the console using console.log.

gistlibby LogSnag