How to get the last 7 days from now using moment.js?

Member

by lew , in category: Javascript , 9 months ago

How to get the last 7 days from now using moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by daisha , 9 months ago

@lew 

To get the date range for the last 7 days from now using Moment.js, you can use the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
const moment = require('moment');

// Get the current date
let currentDate = moment();

// Get the date 7 days ago from now
let dateSevenDaysAgo = moment().subtract(7, 'days');

console.log('Current Date:', currentDate.format('YYYY-MM-DD'));
console.log('Date 7 days ago:', dateSevenDaysAgo.format('YYYY-MM-DD'));


This code snippet first creates a Moment.js object for the current date. It then calculates the date 7 days ago by subtracting 7 days from the current date. Finally, it outputs the current date and the date from 7 days ago in the 'YYYY-MM-DD' format.

Related Threads:

How to check if two dates are more than 7 days apart using moment.js?
How to get last 1 hour to last 10 days data in mysql?
How to get last seven days in laravel?
How to convert date to hours from now using moment.js?
How to find days left in a quarter using moment.js?
How to add days using moment.js with format of mm/dd/yyyy?