Moment JS - how to subtract 7 days from current date?
Solution 1:
May be:
dateTo = moment().format('YYYY-MM-DD');
dateFrom = moment().subtract(7,'d').format('YYYY-MM-DD');
moment#subtract
Solution 2:
The date object, when casted, is in milliseconds. so:
dateFrom = moment(Date.now() - 7 * 24 * 3600 * 1000).format('YYYY-MM-DD');
Solution 3:
for a date picker y use
first_day: moment()
.subtract(5, "day")
.endOf("day")
.toDate(),
last_day: moment()
.endOf("day")
.toDate(),
Solution 4:
The question is outdated so does the solution.
Using Moment v2.29 +
You can add or subtract days using following ways
moment().day(-7); // last Sunday (0 - 7)
moment().day(0); // this Sunday (0)
moment().day(7); // next Sunday (0 + 7)
moment().day(10); // next Wednesday (3 + 7)
moment().day(24); // 3 Wednesdays from now (3 + 7 + 7 + 7)
For more please refer the official documentation https://momentjs.com/docs/#/get-set/
Solution 5:
You can use:
moment().subtract(1,'w')
to subtract one week (7 days) from the current date.
NOTE:
1. w for week
2. d for days
3. m for month
4. y for year