如果第 2 个星期六出现在前 9 天,则获取该月第 1 个和第 3 个星期六的列表

Get List of 1st and 3rd Saturday of month if 2nd Saturday Appear in first 9 days

我想获取一年中所有第 2 个和第 4 个星期六的列表。但是,如果第 2 个星期六出现在一个月的前 9 天,则 return 列出该月的第 3 个和第 5 个星期六。到目前为止,我得到了全年所有第 2 个和第 4 个星期六的清单。但是,如果第二个星期六出现在一个月的前 9 天,我将无法获得第 3 个和第 5 个星期六。

class Program
{
    static void Main(string[] args)
    {
        List<DateTime> MyCalendar = new List<DateTime>(); //create list
        DateTime currDate = new DateTime(2017, 1, 1); //initial value
        //add days to MyCalendar
        while (currDate <= new DateTime(2017, 12, 31))
        {
            MyCalendar.Add(currDate);
            currDate = currDate.AddDays(1);
        }
        //method to get 2. and 4. saturday in month
        var result = MyCalendar.Where(x => x.DayOfWeek == DayOfWeek.Saturday)
                        .GroupBy(x => x.Month)
                        .SelectMany(grp =>
                            grp.Select((d, counter) => new
                            {
                                Month = grp.Key,
                                PosInMonth = counter + 1,
                                Day = d
                            }))

                        .Where(x => x.PosInMonth == 2 || x.PosInMonth == 4)
                        .ToList();

        foreach (var d in result)
        {
            Console.WriteLine("{0} {1} {2}", d.Month, d.PosInMonth, d.Day);
        }

        Console.Read();

    }

}

程序输出

Out of Program

这个怎么样?

using System;
using System.Collections.Generic;

namespace Demo
{
    class Program
    {
        static void Main()
        {
            foreach (var saturday in FilteredSaturdays(DateTime.Now, new DateTime(2017, 12, 31)))
            {
                Console.WriteLine(saturday);
            }
        }

        public static IEnumerable<DateTime> FilteredSaturdays(DateTime start, DateTime end)
        {
            DateTime startMonth = new DateTime(start.Year, start.Month, 1);
            DateTime endMonth = new DateTime(end.Year, end.Month, 1).AddMonths(1);

            for (DateTime month = startMonth; month < endMonth; month = month.AddMonths(1))
            {
                // Work out date of last saturday in the month.

                DateTime lastDayOfMonth = month.AddMonths(1).AddDays(-1);
                DateTime lastSaturdayOfMonth = lastDayOfMonth.AddDays(-(((int)lastDayOfMonth.DayOfWeek+1)%7));

                // Return saturday 2 weeks before last saturday of month, and last saturday of month.

                yield return lastSaturdayOfMonth.AddDays(-14);
                yield return lastSaturdayOfMonth;
            }
        }
    }
}

这比查看一年中的每一天来判断是否是星期六效率更高!

[编辑] 看来实际要求是每个月的最后一个星期六和那个星期六前两周的星期六,所以我相应地更新了我的解决方案。

另一种方法

            DateTime currDate = new DateTime(2017, 1, 1); //initial value
            int dayOfWeek = (int)currDate.DayOfWeek;
            currDate = currDate.AddDays(6 - dayOfWeek);
            //add days to MyCalendar
            while (currDate <= new DateTime(2017, 12, 31))
            {
                MyCalendar.Add(currDate);
                currDate = currDate.AddDays(7);
            }
            var result = MyCalendar.GroupBy(x => x.Month).Select(x => x.Skip(1).First().Day <= 9 ? new DateTime[] { x.Skip(2).First(), x.Skip(4).First() } : new DateTime[] { x.Skip(1).First(), x.Skip(3).First() }).SelectMany(x => x).ToList();

            foreach (var d in result)
            {
                Console.WriteLine("{0} {1}", d.Month, d.Day);
            }

            Console.Read();

这是一个快速而肮脏的解决方案;我承认它可以更优化。

执行查找的方法:

private static IEnumerable<DateTime> GetWeekDayOfMonth(DateTime monthToCheck, DayOfWeek weekDayToFind)
{
    var year = monthToCheck.Year;
    var month = monthToCheck.Month;
    var dayCount = DateTime.DaysInMonth(year, month);
    var daysList = Enumerable.Range(1, dayCount)
                                    .Select(day => new DateTime(year, month, day))
                                    .Where(date => date.DayOfWeek == weekDayToFind)
                                    .ToList<DateTime>();
    // Loop with 2 increment
    int lookupStart = 1;
    int loopCount = 0;
    if (daysList[1].Day <= 9)
    {
        lookupStart = 2;
    }

    for (var i = lookupStart; i < daysList.Count(); i = i + 2) 
    {
        if (loopCount < 2)
        {
            yield return daysList[i];
            loopCount++;
        }
    }
}

调用它的代码如下:

private static void FindWeekDays()
{
    DateTime dateToCheck = new DateTime(2017, 1, 1);
    List<DateTime> dayList = new List<DateTime>();
    while (dateToCheck.Year <= 2017)
    {
        dayList.AddRange(GetWeekDayOfMonth(dateToCheck, DayOfWeek.Saturday));
        dateToCheck = dateToCheck.AddMonths(1);
    }

    dayList.ForEach(date => Console.WriteLine(date.ToString()));
}

主要的:

static void Main(string[] args)
{
    FindWeekDays();
}

将为您提供以下 2017 年的结果(可以更改):