Just a quick solution, there could be other ways of doing this too.
a!localVariables(
local!year: 2024,
local!month: 8,
/* Get the first and last day of the given month */
local!firstDayOfMonth: date(local!year, local!month, 1),
local!lastDayOfMonth: eomonth(local!firstDayOfMonth, 0), /* End of the month */
local!result:a!forEach(
items: enumerate(day(local!lastDayOfMonth)) + 1,
expression: a!localVariables(
local!currentDate: date(local!year, local!month, fv!item), /* Current day in the loop */
local!weekStart:if(
weekday(local!currentDate, 2) = 1, /* If the current day is Monday */
local!currentDate, /* It's the start of the week */
local!currentDate - weekday(local!currentDate, 2) + 1 /* Go back to the previous Monday */
),
local!weekEnd: if(
weekday(local!currentDate, 2) = 7, /* If the current day is Sunday */
local!currentDate, /* It's the end of the week */
local!currentDate + (7 - weekday(local!currentDate, 2)) /* Move forward to the upcoming Sunday */
),
/* Handle boundary conditions if the start or end is outside the month */
local!adjustedWeekStart: if(local!weekStart < local!firstDayOfMonth, local!firstDayOfMonth, local!weekStart),
local!adjustedWeekEnd: if(local!weekEnd > local!lastDayOfMonth, local!lastDayOfMonth, local!weekEnd),
/* Return the start and end date for the week */
{
start: local!adjustedWeekStart,
end: local!adjustedWeekEnd
}
)
),
union(local!result,local!result)
)