Dushanba kuni yakshanbaga borib, ushbu oyda

Bir oydan buyon men joriy oyda yakshanba kunlarini bilishni istayman, masalan: 2011-yilning oktabr oylarida

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 

kunlarning bu ketma-ketligi shu kunlarda 3-oktabr 2011,4-okt-2011 kabi .... 29-oct-11 va boshqalar va hokazo kabi qator formatida yoki ma'lumotlarga ega bo'lish kerak.

1

2 javoblar

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();

Bu joriy oydagi kunlar soniga qarab, har birining uchun DateTime obyekti yaratiladi, keyin faqat yakshanba bo'lmagan qatorlarni sanani qaytarib beradi.

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).

Tahrirlash:

Mana, oldingi LINQ echimiga o'xshash narsani hal qiladigan .NET 2 echim.

        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)
                dates.Add(date);

            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
qo'shib qo'ydi
@Nathan Men javobni yangiladim. NET 2.0 ni ham o'z ichiga oladi.
qo'shib qo'ydi muallif Johannes Kommer, manba
2005 yildan beri foydalanmoqdaman
qo'shib qo'ydi muallif Nathan, manba
u katta rahmat
qo'shib qo'ydi muallif Nathan, manba

eng qulay:

        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()));
        Console.ReadLine();
0
qo'shib qo'ydi
Iltimos, javobingizni shakllantirishingiz mumkin. Hozirgi holatda osongina ukish mumkin emas
qo'shib qo'ydi muallif Rahul Vishwakarma, manba