选择每月的第四个星期日

时间:2010-09-01 01:24:58

标签: c# asp.net

我现在被困了一段时间,现在需要你的帮助。

我想在每个月的第四个星期日显示,例如从2010年9月1日到2011年8月31日

我只想在下拉列表中找到第四个星期日,如何使用asp.net C#

此致

3 个答案:

答案 0 :(得分:8)

这是一种使用一点LINQ的方法,以及第四个星期日将在一个月的22日和28日之间发生的知识。

DateTime startDate = new DateTime(2010, 9, 1);
DateTime endDate = startDate.AddYears(1).AddDays(-1);

List<DateTime> fourthSundays = new List<DateTime>();

DateTime currentDate = startDate;
while (currentDate < endDate)
{
    // we know the fourth sunday will be the 22-28
    DateTime fourthSunday = Enumerable.Range(22, 7).Select(day => new DateTime(currentDate.Year, currentDate.Month, day)).Single(date => date.DayOfWeek == DayOfWeek.Sunday);
    fourthSundays.Add(fourthSunday);
    currentDate = currentDate.AddMonths(1);
}

然后,您可以将List<DateTime>绑定到下拉列表或跳过列表本身,以便在您将这些项目生成到下拉列表时添加这些项目,如下所示。

yourDropdown.Items.Add(new ListItem(fourthSunday.ToString()));

对于咯咯笑,你可以在LINQ语句中完成整个事情并跳过(大部分)变量。

DateTime startDate = new DateTime(2010, 9, 1); 
IEnumerable<DateTime> fourthSundays =
    Enumerable.Range(0, 12)
    .Select(item => startDate.AddMonths(item))
    .Select(currentMonth =>
        Enumerable.Range(22, 7)
        .Select(day => new DateTime(currentMonth.Year, currentMonth.Month, day))
        .Single(date => date.DayOfWeek == DayOfWeek.Sunday)
    );

答案 1 :(得分:1)

无聊所以你走了。两个辅助方法一检索周(如果存在),另一个迭代几个月

class Program
{
    static void Main(string[] args)
    {
        DateTime startDate = new DateTime(2010, 09, 1);
        foreach(DateTime dt in EachMonth( new DateTime(2010, 09, 1), new DateTime(2011, 09, 1))){
            DateTime? result = GetDayByWeekOffset(DayOfWeek.Sunday, dt, 4);
           Console.WriteLine("Sunday:" + (result.HasValue?result.Value.ToString("MM-dd-yyyy"):"null"));
        }
        Console.ReadKey();
    }

    public static DateTime? GetDayByWeekOffset(DayOfWeek day, DateTime month, int weekOffSet)
    {
        //First day of month
        DateTime firstDayOfMonth = month.AddDays((-1 * month.Day) + 1);

        // 
        int daysOffSet;
        daysOffSet= ((int)day + 7 - (int)firstDayOfMonth.DayOfWeek) % 7;
        DateTime firstDay = month.AddDays(daysOffSet);

        // Add the number of weeks specified
        DateTime resultDate = firstDay.AddDays((weekOffSet - 1) * 7);

        if (resultDate.Month != firstDayOfMonth.Month){
            return null;
        }else{
            return resultDate;
        }
    }

    public static IEnumerable<DateTime> EachMonth(DateTime from, DateTime thru)
    {
        for (var month = from.Date; month.Date <= thru.Date; month = month.AddMonths(1))
            yield return month;
    }
}

答案 2 :(得分:0)

安东尼的答案很好,我很喜欢。作为替代方案,这里有一个参数化的方法,用于星期几和星期数(即如果你需要其他组合,如第4个星期日,第3个星期五等)和一些评论。

就你的情况称之为:

List<DateTime> sundays = DateInstances(new DateTime(2010, 9, 1), new DateTime(2011, 8, 31), DayOfWeek.Sunday, 4);

方法本身:

public List<DateTime> DateInstances(DateTime start, DateTime end, DayOfWeek day, int weeks)
{
    if (start > end)
        throw new ArgumentOutOfRangeException("end", "The start date must occur before the end date");

    List<DateTime> results = new List<DateTime>();

    DateTime temp = start;
    while (temp < end)
    {
        DateTime firstWeekday = new DateTime(temp.Year, temp.Month, 1);

        //increment to the given day (i.e. if we want the 4th sunday, we must find the first sunday of the month)
        while (firstWeekday.DayOfWeek != day)
            firstWeekday = firstWeekday.AddDays(1);

        //add the number of weeks (note: we already have the first instance, so subtract 1)
        firstWeekday = firstWeekday.AddDays(7 * (weeks - 1));

        //make sure we haven't gone over to the next month
        if (firstWeekday.Month == temp.Month)
            results.Add(firstWeekday);

        //let's not loop forever ;)
        temp = temp.AddMonths(1);
    }

    return results;
}