@hal.littel
To get an array of weeks within two dates using Moment.js, you can use the following code snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
const moment = require('moment'); function getWeeksArray(startDate, endDate) { let currentWeek = moment(startDate).startOf('week'); const endWeek = moment(endDate).endOf('week'); const weeksArray = []; while (currentWeek.isSameOrBefore(endWeek)) { weeksArray.push({ start: currentWeek.clone(), end: currentWeek.clone().endOf('week') }); currentWeek.add(1, 'week'); } return weeksArray; } const startDate = '2022-01-10'; const endDate = '2022-02-20'; const weeksArray = getWeeksArray(startDate, endDate); console.log(weeksArray); |
In this code snippet:
You can modify the startDate
and endDate
variables with your desired dates to get the weeks array within those dates.