问题
Using moment().subtract(1, 'days').format("YYYY-MM-DD")
i can get last x days from the current date. So how can i get all days from last month or last three months..?
回答1:
If I understand your question correctly, this can be achieved via two steps. First calculate the moment that marks the starting date of the prior month:
var prevMonth = moment().subtract(1, 'month').startOf('month');
var prevMonthDays = prevMonth.daysInMonth();
Then, iterate over the range 0...prevMonthDays
, caclulating dates per day of that range, relative to the start of the previous month prevMonth
:
var prevMonthDay = prevMonth.clone().add(i, 'days').format("YYYY-MM-DD");
Something like this should achieve what you require:
// Get moment at start date of previous month
var prevMonth = moment().subtract(1, 'month').startOf('month');
var prevMonthDays = prevMonth.daysInMonth();
// Array to collect dates of previous month
var prevMonthDates = [];
for (var i = 0; i < prevMonthDays; i++) {
// Calculate moment based on start of previous month, plus day offset
var prevMonthDay = prevMonth.clone().add(i, 'days').format("YYYY-MM-DD");
prevMonthDates.push(prevMonthDay);
}
console.log(prevMonthDates)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
Hope this helps!
Update
To calculate the list of dates starting many month past through to the beginning of the current month, the code above can be generalized as follows:
var monthsPast = 4;
var prevMonthDays = 0;
var prevMonth = moment();
// Iterate over number of months past that we want to collect dates for
for (var i = 0; i < monthsPast; i++) {
// Calculate the moment at the start of a previous month
var prevMonthStart = moment().subtract(i + 1, 'month').startOf('month');
// Increment total range to collect dates over, and update prevMonth
// to current calculated moment which represents oldest month start
prevMonthDays += prevMonthStart.daysInMonth();
prevMonth = prevMonthStart;
}
// Array to collect dates of previous month
var prevMonthDates = [];
for (var i = 0; i < prevMonthDays; i++) {
// Calculate moment based on start of previous month, plus day offset
var prevMonthDay = prevMonth.clone().add(i, 'days').format("YYYY-MM-DD");
prevMonthDates.push(prevMonthDay);
}
console.log(prevMonthDates)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
回答2:
This is how to get the number of days in current month:
const numberOfDaysInCurrentMonth = moment().daysInMonth();
So this is how you can get the number of days for the last month:
const numberOfDaysInLastMonth = moment().subtract(1, 'months').daysInMonth();
Now you need the start of last month:
const startOfLastMonth = moment().subtract(1, 'months').startOf('month');
And you are ready to start iterating and build your list:
const numberOfDaysInLastMonth = moment().subtract(1, 'months').daysInMonth();
const startOfLastMonth = moment().subtract(1, 'months').startOf('month');
for (let i = 0; i < numberOfDaysInLastMonth; i = i + 1) {
console.log(startOfLastMonth.format('YYYY MMM DD'));
startOfLastMonth.add(1, 'days');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
For the last 3 months, just change you start date and number of Days:
const numberOfDaysInLast3Months = moment().subtract(1, 'months').daysInMonth() +
moment().subtract(2, 'months').daysInMonth() +
moment().subtract(3, 'months').daysInMonth();
const startDate = moment().subtract(3, 'months').startOf('month');
for (let i = 0; i < numberOfDaysInLast3Months; i = i + 1) {
console.log(startDate.format('YYYY MMM DD'));
startDate.add(1, 'days');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
来源:https://stackoverflow.com/questions/54088255/how-to-get-list-of-days-for-last-month-last-three-months-in-momentjs