0

How can we get all week start date and end date of current month?

Below code shows only current week start date and end date

var startOfWeek = moment().startOf("isoWeek").toDate();
var endOfWeek = moment().endOf("isoWeek").toDate();
console.log(startOfWeek)
console.log(endOfWeek)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.0/moment.min.js" integrity="sha512-Izh34nqeeR7/nwthfeE0SI3c8uhFSnqxV0sI9TvTcXiFJkMd6fB644O64BRq2P/LA/+7eRvCw4GmLsXksyTHBg==" crossorigin="anonymous"></script>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
abhi
  • 21
  • 5
  • What are you calling "week of the current month"? Is that any ISO week that has any days in the current month? Only those that are predominantly in the current month? Those that start in the current month? etc. – RobG Sep 29 '20 at 22:47

3 Answers3

0

Just add / subtract 7 days while you are in current month to get following and preceeding weeks.

let current = startOfWeek;

do {
  current = current.subtract(7, "days");  //or .add(7, "days") to get next week
} while (current.month() === startOfWeek.month())
Code Spirit
  • 3,992
  • 4
  • 23
  • 34
0

Is this interesting? - no use of moment

No sure what you want to do with weeks that start before the 1st or end in the next month

const d = new Date(),
month = d.getMonth(),
yyyy = d.getFullYear(),
end = new Date(yyyy,month+1,0).getDate();
const startEnd = Array.from(Array(end+1).keys()).slice(1)
  .reduce((acc,day) => {
  const d = new Date(yyyy,month,day,15,0,0,0);
  if (d.getDay() === 1) acc.push({start:d});
  else if (d.getDay() === 6 && acc[acc.length-1]) acc[acc.length-1]["end"] = d;
  return acc
},[])

console.log(startEnd)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • For me this gives 6 day weeks, e.g. week 1 is Monday 7 Sep to Saturday 12 Sep, Sunday is omitted. The last week has a start (Mon 28) but no end. :-( – RobG Sep 29 '20 at 22:57
  • I kno - I asked you what is expected result? – mplungjan Sep 30 '20 at 04:06
0

The question mentions ISO weeks, so I guess you want any ISO week of the year that falls in the current month. You can do that by getting the 1st of the month, setting to the previous Monday (or same day if it is a Monday), then iterating over the weeks until you get to the next month. E.g.

// Month is calendar month number
function getISOWeeksInMonth(month = new Date().getMonth() + 1, year = new Date().getFullYear()) {
  // Start at 1st of month
  let weekStart = new Date(year, month - 1, 1);
  // Set to prior Monday
  weekStart.setDate(weekStart.getDate() - (weekStart.getDay() || 7) + 1);
  // Get end of week
  let weekEnd = new Date(weekStart);
  weekEnd.setDate(weekEnd.getDate() + 6);
  // Get week number of 1st week
  let weekNum = getWeekNumber(weekStart)[1];
  // Create weeks array
  let weeks = [];
  while (weekStart.getMonth() < month) {
    weeks.push({
      weekNum : weekNum++,
      start: new Date(weekStart),
      end: new Date(weekEnd)
    });
    weekStart.setDate(weekStart.getDate() + 7);
    weekEnd.setDate(weekEnd.getDate() + 7);
  }
  return weeks;
}

// Get ISO week number - from https://stackoverflow.com/a/6117889/257182
function getWeekNumber(d) {
    d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
    d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7));
    var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
    var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7);
    return [d.getUTCFullYear(), weekNo];
}

// ISO weeks of current month
getISOWeeksInMonth().forEach(week => console.log(
   'Week : ' + week.weekNum +
   '\nStart: ' + week.start.toDateString() + 
   '\nEnd  : ' + week.end.toDateString())
);

// ISO weeks in Feb 2021 (starts on a Monday)
getISOWeeksInMonth(2, 2021).forEach(week => console.log(
   'Week : ' + week.weekNum +
   '\nStart: ' + week.start.toDateString() + 
   '\nEnd  : ' + week.end.toDateString())
);
RobG
  • 142,382
  • 31
  • 172
  • 209