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

The subdays function from date-fns is used to subtract a specified number of days from a given date. Here's how you can use it in JavaScript:

  1. First, install the date-fns library using npm or yarn:
# using npm
npm install date-fns

# using yarn
yarn add date-fns
65 chars
6 lines
  1. Import the subdays function in your JavaScript file:
index.tsx
import { subDays } from 'date-fns';
36 chars
2 lines
  1. Use the subdays function to subtract days from a date:
index.tsx
const today = new Date(); // e.g. 2021-09-01

const threeDaysAgo = subDays(today, 3); // subtract 3 days from today's date
console.log(threeDaysAgo); // output: 2021-08-29T22:00:00.000Z (depending on your timezone)
215 chars
5 lines

The subdays function takes two arguments: the first is the date you want to subtract days from, and the second is the number of days to subtract (as an integer). The function returns a new Date object with the specified number of days subtracted.

gistlibby LogSnag