Hi everyone, can anyone tell me how to know first week of the month, on which day we consider it's a first week of the month
Can anyone tell me how to know first week of the month
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.

Vishal YelveRank #178 · 13.6K pointsPosted Sep 29, 2022, 10:34 AM EDT
Hi Mady,
try this, it will return you the week number
static int GetWeekNumberOfMonth(DateTime date) { date = date.Date; DateTime firstMonthDay = new DateTime(date.Year, date.Month, 1); DateTime firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7); if (firstMonthMonday > date) { firstMonthDay = firstMonthDay.AddMonths(-1); firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7); } return (date - firstMonthMonday).Days / 7 + 1; }Output
Console.WriteLine(GetWeekNumberOfMonth(new DateTime(2022, 9, 5))); // 1 Console.WriteLine(GetWeekNumberOfMonth(DateTime.Now); // 4