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

You can use the previousDay function from the date-fns library in JavaScript to get the date of the previous day. Here's how to use it:

First, install the date-fns library via npm:

npm install date-fns
21 chars
2 lines

Then, import the previousDay function and use it to get the date of the previous day:

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

const today = new Date();
const previousDayDate = previousDay(today);
console.log(previousDayDate);
// Outputs the date of the previous day in the format "YYYY-MM-DD"
208 chars
7 lines

You can also pass a second argument to the previousDay function to specify the number of days to go back:

index.tsx
const today = new Date();
const twoDaysAgo = previousDay(today, 2);
console.log(twoDaysAgo);
// Outputs the date two days ago in the format "YYYY-MM-DD"
153 chars
5 lines

gistlibby LogSnag