C#从今天的日期开始下一个星期五的日期

时间:2011-11-02 13:26:10

标签: c# asp.net datetime date

我有以下内容,但它不是我现在所需要的 - 它返回过去一个月的所有星期五的日期。

    public static IEnumerable<DateTime> ReturnNextNthWeekdaysOfMonth(DateTime dt, DayOfWeek weekday, int amounttoshow = 4)
    {
        var days =
            Enumerable.Range(1, DateTime.DaysInMonth(dt.Year, dt.Month)).Select(
                day => new DateTime(dt.Year, dt.Month, day));

        var weekdays = from day in days
                       where day.DayOfWeek == weekday
                       orderby day.Day ascending
                       select day;

        return weekdays.Take(amounttoshow);
    }

然而,我现在想要从今天的日期返回下一个星期五的日期,而不是他们所在的月份。

我有点卡住......任何帮助都非常感激。

7 个答案:

答案 0 :(得分:2)

尝试这个......

    public static List<DateTime> ReturnNextNthWeekdaysOfMonth(DateTime dt, DayOfWeek weekday, int amounttoshow = 4)
    {
        List<DateTime> list = new List<DateTime>();

        dt = dt.AddDays(weekday - dt.DayOfWeek);//set to the first day in the list

        if (weekday <= dt.DayOfWeek)
            dt = dt.AddDays(7);

        for (int i = 0; i < amounttoshow; i++)
        {
            list.Add(dt);
            dt = dt.AddDays(7);
        }

        return list;
    }

请注意,就目前而言,如果您在当天通过,那么列表中的第一个日期将是下周,而不是今天。如果您希望今天作为此实例中的第一个日期包含在内,则可以使用以下代码....

    public static IEnumerable<DateTime> ReturnNextNthWeekdaysOfMonth(DateTime dt, DayOfWeek weekday, int amounttoshow = 4)
    {
        List<DateTime> list = new List<DateTime>();

        if (weekday < dt.DayOfWeek)
            dt = dt.AddDays(7);

        dt = dt.AddDays(weekday - dt.DayOfWeek);

        for (int i = 0; i < amounttoshow; i++)
        {
            list.Add(dt);
            dt = dt.AddDays(7);
        }

        return list;
    }

答案 1 :(得分:2)

public static IEnumerable<DateTime> ReturnNextNthWeekdaysOfMonth(DateTime dt, DayOfWeek weekday, int amounttoshow = 4)
{
    // Find the first future occurance of the day.
    while(dt.DayOfWeek != weekday)
        dt = dt.AddDays(1);

    // Create the entire range of dates required. 
    return Enumerable.Range(0, amounttoshow).Select(i => dt.AddDays(i * 7));
}

这首先查找匹配weekday的第二天,然后继续创建amounttoshow DateTime实例,每个实例都比上一个实例开始,从找到的日期开始。

答案 2 :(得分:1)

无需担心LINQ:

public static IEnumerable<DateTime> ReturnNextNthWeekdaysOfMonth(DateTime dt, DayOfWeek weekday, int amounttoshow = 4)
{
    while(dt.DayOfWeek != weekday)
        dt = dt.AddDays(1);

    for (int i = 0; i < amounttoshow; i++)
    {
        yield return dt;
        dt = dt.AddDays(7);
    }
}

答案 3 :(得分:0)

将最后一行更改为

return weekdays.Where((x, i) => i % N == 0);

答案 4 :(得分:0)

替换完整的方法体
return (from z in Enumerable.Range (0, amounttoshow) 
let b = (from x in Enumerable.Range (0, 6) where DateTime.Now.AddDays (x).DayOfWeek == weekday select DateTime.Now.AddDays (x)).First() 
select b.AddDays (z * 7));

答案 5 :(得分:0)

试试这个

public static IEnumerable<DateTime> ReturnNextNthWeekdaysOfMonth(DateTime dt, DayOfWeek weekday, int amounttoshow = 4)
  {
     // get the difference from the weekday
     int diff = dt.DayOfWeek - weekday;

     // amounttoshow * 7 is the number of days in a week (28 to get 4 weeks)
     var days =
         Enumerable.Range(1, amounttoshow * 7 + diff).Select(            
             day => DayOfYear(dt, day));

     var weekdays = from day in days
                    where day.DayOfWeek == weekday
                    orderby day.Day ascending
                    select day;

     return weekdays.Take(amounttoshow);
  }

  // returns the day in datetime
  public static DateTime DayOfYear(DateTime dt, int day)
  {
     DateTime firstDayOfYear = new DateTime(dt.Year, 1, 1);
     DateTime dateTime = firstDayOfYear.AddDays(dt.DayOfYear - 1 + day);

     return dateTime;
  }

获取值

DateTime d = new DateTime(2011, 11, 5);
IEnumerable<DateTime> ie = ReturnNextNthWeekdaysOfMonth(d, DayOfWeek.Friday, 4);

System.Diagnostics.Debug.WriteLine(ie.First().ToString());

答案 6 :(得分:0)

更高速的替代方案(算术而不是第一个日期的搜索循环,并在循环外移动昂贵的乘法):

public static IEnumerable<DateTime> ReturnNextNthWeekdaysOfMonth(DateTime dt, 
    DayOfWeek weekday, int amounttoshow = 4)
{
    var day = dt.AddDays(weekday > dt.DayOfWeek 
        ? weekday - dt.DayOfWeek
        : 7 - weekday - dt.DayOfWeek);
    var days = new List<DateTime>();
    for(var until = day.AddDays(7 * amounttoshow); 
        day < until; 
        day = day.AddDays(7))
        days.Add(day);
    return days.ToArray();
}