2011-11-02 222 views
3

我有以下,但它不完全是我現在需要 - 它返回所有星期五的日期傳入月C#獲取下一個第N個星期五日期從今天的日期

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

不過,我現在想返回從今天的日期,今年的第N個星期五日期,他們是在月的無關。

而我有點卡住...任何幫助極大讚賞。

+1

您是否看到[this](http://angstrey.com/index.php/2009/04/25/finding-the-next-date-for-day-of-week/)? – m0skit0

+2

愚蠢的問題,但你爲什麼不只是1)確定下一個星期五2)通過添加方法N-1次添加7天,將您的答案放入列表?這對我來說似乎是一個奇怪的使用LINQ。 – JSWork

回答

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然後繼續牛逼o創建amounttoshow DateTime實例,每個實例比搜索日期開始的時間都多7天。

0

更改最後一行

return weekdays.Where((x, i) => i % N == 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星期二,現在的第一個結果應該是過去使用'dt = dt.AddDays(weekday - dt.DayOfWeek)的週二;' –

+0

@ J.Kommer:非常感謝,有更新的代碼修復...可能的優化,以遵循;) – musefan

+1

仍然不好。先進行if檢查,然後製作<而不是<=。 編輯:(錯誤假設你想包括當前的日子,如果它是正確的「DayOfWeek」開始) – JSWork

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)); 
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); 
    } 
} 
+1

var day = dt.DayOfWeek == weekday? dt:dt.AddDays(weekday - dt.DayOfWeek);是錯的。 1)工作日可以比dt.DayOfWeek更大。這將導致你返回錯誤的結果 2)等號檢查是毫無意義的。如果weekday == dt.DayOfWeek是真的,你只需要添加0天。 – JSWork

+0

我想你會遭受同樣的問題,因爲我的原始答案([見我的評論](http://stackoverflow.com/questions/7981106/c-sharp-get-next-nth-fridays-date-from-todays-日期/ 7981175#7981175)) – musefan

+0

@musefan的確你是對的。當我回到電腦時,我會修改開始條件,但其餘的邏輯應該沒問題 –

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

教你想使用查詢... – Khan

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(); 
} 
相關問題