获取一周中的几天,按一个月中的一天

Get days of a week, by a day of the month

我一直在 Whosebug 上四处寻找,但找不到如何在 C# 上解决以下问题。 我需要一种方法,我将发送一个 int(一个月中的某一天),它总是 return 从周一到周五所属的一个月中的几天。

例如: 今天是 8 号,我需要方法 return 是一个包含以下数字 (3, 4, 5, 6, 7) 的数组。

或者想象一下,即使它是 8,我发送给这个方法 19,它 returns (17, 18, 19, 20, 21) 对应17号到周一,18号到周二....

我没有任何代码,因为我不知道该怎么做。 希望你能帮助我!

非常感谢!

编辑:假设我发送了一个简单的日期格式 08/05/2021,而不是仅仅发送日期

private DateTime[] weekByDay(DateTime date)
{
  DateTime[] week = new DateTime[5];
  while (date.DayOfWeek != DayOfWeek.Monday) //while day is not monday
  {
    date = date.AddDays(-1); //substract 1 day to date
  }
  week[0] = date; //add the current day (monday), to the array
  for (int i = 1; i < 5; i++) //add the next day till Friday
  {
   date = date.AddDays(1);
   week[i] = date;
  }
  Console.WriteLine("MONDAY was " + week[0]);
  Console.WriteLine("TUESDAY was " + week[1]);
  Console.WriteLine("WEDNESDAY was " + week[2]);
  Console.WriteLine("THURSDAY was " + week[3]);
  Console.WriteLine("FRIDAY was " + week[4]);
  return week;
}