get the date 5 months ago in javascript

Here's how you can get the date 5 months ago using JavaScript and the Moment.js library:

index.tsx
// Install moment.js via npm or load it from a CDN or locally
// Then require/import it in your JS file

const moment = require('moment');

// Subtract 5 months from the current date

const fiveMonthsAgo = moment().subtract(5, 'months').toDate();

console.log(fiveMonthsAgo); // returns the date 5 months ago from today
320 chars
11 lines

Alternatively, without using Moment.js, you can do:

index.tsx
const fiveMonthsAgo = new Date();
fiveMonthsAgo.setMonth(fiveMonthsAgo.getMonth() - 5);

console.log(fiveMonthsAgo); // returns the date 5 months ago from today
161 chars
5 lines

gistlibby LogSnag