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

To use the previousthursday function from date-fns library in JavaScript, you first need to install the library using npm.

npm install date-fns
21 chars
2 lines

After installing, you can import the previousthursday function in your JavaScript file using the following code:

index.tsx
const { previousthursday } = require('date-fns');
50 chars
2 lines

You can then call the previousthursday function, passing a Date or a string representing a valid date, to get the date of the previous Thursday.

index.tsx
const prevThursdayDate = previousthursday(new Date()); // returns the date of the previous Thursday
100 chars
2 lines

You can also pass a second optional parameter to the previousthursday function, which is an object containing options for the function.

index.tsx
const prevThursdayDateWithOptions = previousthursday(new Date(), { weekStartsOn: 1 }); // returns the date of the previous Thursday considering Monday as the first day of the week
180 chars
2 lines

In the example above, the weekStartsOn option is passed to previousthursday function with a value of 1, which indicates that Monday is considered as the first day of the week.

gistlibby LogSnag