0

so the problem is this: I managed to get the number of weeks that make up a month (for example: May 2022 is made up of 6 weeks counting from day 1 to day 31 without exception). So having the number of weeks that make up a month I need to know, knowing also the number of the month and the year, which will be the first and last day of the selected week.

I tried to do some research but I only find solutions that use the Calendar.GetWeekOfYear method but, as I said before, I have the week number for the single month not the total for the year.

I hope you can help me. Thanks in advance.

st4ticv0id
  • 39
  • 6
  • I did it [like this](https://github.com/MintPlayer/mintplayer-ng-bootstrap/blob/master/libs/mintplayer-ng-bootstrap/src/lib/services/calendar-month/calendar-month.service.ts#L84), but I think [this is a better approach](https://stackoverflow.com/a/38064/8941307) (see the comment below the answer) – Pieterjan May 15 '22 at 14:11
  • Thanks, but unfortunately my problem is precisely the fact that I do not start from a complete date (dd/MM/yyyy) but from a date that contains only month and year (MM/yyyy) and I have to use this 'incomplete date' together with the number of a week of the selected month (week 2 of 6 weeks for example) to obtain the Monday and Sunday of that same week. Thanks anyway :). – st4ticv0id May 15 '22 at 14:20

1 Answers1

1

If I understood correctly it should look something like this

var week = 2;
var month = 5;
var year = 2022;

var firstDayOfMonth = new DateTime(year, month, 1);
var dayOfWeek = firstDayOfMonth.DayOfWeek;
var diffToMonday = (7 + (dayOfWeek - DayOfWeek.Monday)) % 7;
var firstMonday = firstDayOfMonth.AddDays(-diffToMonday);

var requestedMonday = firstMonday.AddDays(7 * (week - 1));
var requestedSunday = firstMonday.AddDays(7 * week - 1);
GuyVdN
  • 690
  • 4
  • 14