# 今月の月曜日を土曜日にする

``````3-oct-2011 to 8-oct-2011,
10-OCt-11 to 15-Oct-11,
17-Oct-11 to 22-oct-2011,
24-Oct-2011 to 29-Oct-2011
``````

これらの日のすべてのこれらすべての日は、3-oct-2011,4-oct-2011 .... 29-oct-11などのように、配列形式またはデータテーブルに入る必要があります。

1

## 2 答え

``````var today = DateTime.Today;
var daysInMonth = DateTime.DaysInMonth(today.Year, today.Month);

var dates = Enumerable.Range(1, daysInMonth)
.Select(n => new DateTime(today.Year, today.Month, n))
.Where(date => date.DayOfWeek != DayOfWeek.Sunday)
.ToArray();
``````

これは、現在の月の日数を調べ、それぞれのDateTimeオブジェクトを作成し、配列として日曜日でない日付のみを返します。

``````var today = DateTime.Today;
var daysInMonth = DateTime.DaysInMonth(today.Year, today.Month);

var dates = Enumerable.Range(1, daysInMonth)
.Select(n => new DateTime(today.Year, today.Month, n))
.Where(date => date.DayOfWeek != DayOfWeek.Sunday)
.SkipWhile(date => date.DayOfWeek != DayOfWeek.Monday)
.TakeWhile(date => date.DayOfWeek != DayOfWeek.Monday || (date.DayOfWeek == DayOfWeek.Monday && daysInMonth - date.Day > 7))
.ToArray();
``````

This will do the same, except get rid of any Monday -> Saturday ranges which are not in the current month. (Week started in the previous month, or ends in the next).

ここに私の以前に掲載されたLINQソリューションと同じことをする.NET 2ソリューションがあります。

``````        DateTime today = DateTime.Today;
int daysInMonth = DateTime.DaysInMonth(today.Year, today.Month);

List dates = new List();

bool foundFirst = false;

for (int n = 1; n <= daysInMonth; n++)
{
var date = new DateTime(today.Year, today.Month, n);

//Skip untill we find the first Monday of the month.
if (date.DayOfWeek != DayOfWeek.Monday && !foundFirst)
continue;

foundFirst = true;

//Add all days except Sundays.
if (date.DayOfWeek != DayOfWeek.Sunday)

int remainingDays = daysInMonth - n;

//Verify that there are enough days left in this month to add all days upto the next Saturday.
if (date.DayOfWeek == DayOfWeek.Saturday && remainingDays < 7)
break;
}

DateTime[] dateArray = dates.ToArray();
``````
7

@ Nathan私は.NET 2.0ソリューションも含めるように答えを更新しました。

``````        int month = DateTime.Now.Month;
int year = DateTime.Now.Year;
int days= DateTime.DaysInMonth(year, month);
int totalSaturdays = 0;
for(int i=1;i<=days;i++)
{
var day = new DateTime(year, month, i);
if(day.DayOfWeek==DayOfWeek.Saturday)
{
totalSaturdays++;
}
}
Console.WriteLine(("Total Saturdays ="+totalSaturdays.ToString()));