How To Subtract Months from a Date in JavaScript with date-fns
// import isn't supported in Node v18.11.0 otherwise it would be
// import { subMonths } from 'date-fns'
const dateFns = require( "date-fns");
const { subMonths } = dateFns;
// 31th of January 2023
const date = new Date(2023, 0, 31);
console.log(date);
console.log('Subtract 1 month: ', subMonths(date, 1));
console.log('Subtract 3 months: ', subMonths(date, 3));
console.log('Subtract 9 months: ', subMonths(date, 9));
console.log(date);
no comments