2010-04-22 45 views
0
public static string TimeLine2(this HtmlHelper helper, string myString2) 
{ 
    StringBuilder myString3 = new StringBuilder(); 

    DateTime start = new DateTime(2010, 1, 1); 
    DateTime end = new DateTime(2011, 12, 12); 

    myString3.Append("<table>"); 


     myString3.Append("<tr>"); 
     for (DateTime date = start; date <= end; date = date.AddDays(1)) 
     { 
      DayOfWeek dw = date.DayOfWeek; 
      var g = date.Month; 
      var sun = " "; 

      switch (dw) 
      { 
       case DayOfWeek.Sunday: 
        sun = "S"; 
        break; 
       case DayOfWeek.Monday: 
        sun = "M"; 
        break; 
       case DayOfWeek.Tuesday: 
        sun = "T"; 
        break; 
       case DayOfWeek.Wednesday: 
        sun = "W"; 
        break; 
       case DayOfWeek.Thursday: 
        sun = "T"; 
        break; 
       case DayOfWeek.Friday: 
        sun = "F"; 
        break; 
       case DayOfWeek.Saturday: 
        sun = "S"; 
        break; 
      } 

      myString3.Append("<td>" + sun + " " + g + "</td>"); 
     } 

     myString3.Append("</tr>"); 

     myString3.Append("<tr>"); 
     for (DateTime date = start; date <= end; date = date.AddDays(1)) 
     { 
      var f = date.Day; 
      myString3.Append("<td>" + f + "</td>"); 
     } 
     myString3.Append("</tr>"); 

    myString3.Append("</table>"); 

    return myString3.ToString(); 
} 

基本上,我在這裏是幾個循環,顯示一週中的所有日子以及一個月中的所有日子。這是所有擺放了一張桌子裏面,所以你得到使用DateTime將特定時間跨度的月份分組

 
MTWTFSSMT W T F S S M   M TWTFSSM 

    12345678910 11 12 13 14 + + to 31 1234567 

我試圖讓我的代碼,每月返回到編寫一個方式,我可以拆所有的一週天,這些天在幾個月憑藉其在月均日和一週的每一天,不只是我的時間跨度之間的所有我的幾個月,但分裂他們這麼

 
MAY 
MTWTFSSMTWTFSSMTWTFSSMTWTFSSMTWTF 
    12345678 

JUNE 
MTWTFSSMTWTFSSMTWTFSSMTWTFSSMTWTF 
    123456789 

回答

1

使用LINQ:

DateTime startDate = new DateTime(2010, 1, 1); 
DateTime endDate = new DateTime(2010, 12, 31); 

int monthCount = 
    (endDate.Month - startDate.Month + 1) + 
    (endDate.Year - startDate.Year) * 12; 

Enumerable 
    .Range(0, monthCount) 
    .Select(x => new DateTime(startDate.Year, startDate.Month, 1).AddMonths(x)) 
    .ToList() 
    .ForEach(d1 => 
    { 
     string month = d1.ToString("MMMM"); 
     // here should be your code 
     // to work with months 

     Enumerable 
      .Range(0, d1.AddMonths(1).AddDays(-1).Day) 
      .Select(x => d1.AddDays(x)) 
      .ToList() 
      .ForEach(d2 => 
      { 
       string dayOfWeek = d2.ToString("ddd"); 
       string day = d2.Day.ToString(); 
       // here should be your code 
       // to work with days 
      }); 
    }); 

OK,下一個變體,而不LINQ

StringBuilder sb = new StringBuilder(); 

DateTime startDate = new DateTime(2010, 1, 1); 
DateTime endDate = new DateTime(2012, 12, 31); 

int monthCount = 
    (endDate.Month - startDate.Month + 1) + 
    (endDate.Year - startDate.Year) * 12; 

for (int i = 0; i < monthCount; i++) 
{ 
    DateTime d1 = new DateTime(startDate.Year, startDate.Month, 1).AddMonths(i); 
    string month = d1.ToString("MMMM"); 

    sb.AppendFormat("<p>{0}</p>", month); 

    int daysInMonth = d1.AddMonths(1).AddDays(-1).Day; 
    StringBuilder daysOfWeekRow = new StringBuilder(); 
    StringBuilder daysRow = new StringBuilder(); 
    for (int j = 0; j < daysInMonth; j++) 
    { 
     DateTime d2 = d1.AddDays(j); 
     string dayOfWeek = d2.ToString("ddd"); 
     string day = d2.Day.ToString(); 

     daysOfWeekRow.AppendFormat("<td>{0}</td>", dayOfWeek); 
     daysRow.AppendFormat("<td>{0}</td>", day); 
    } 
    sb.AppendFormat(
     "<table><tr>{0}</tr><tr>{1}</tr></table>", 
     daysOfWeekRow.ToString(), 
     daysRow.ToString() 
    ); 
} 

string result = sb.ToString(); 

您可以根據需要更改輸出格式,我只提供了基本示例。

最主要的是迭代通過必要的日期(使用或不使用LINQ是你的選擇,但你可以同意解決方案LINQ更優雅),並在必要的地方添加自定義格式(我把註釋放在哪裏用第一個例子來做)。

+0

謝謝男人,感謝它,我必須做另一個循環獲得超過一年。 – Calibre2010 2010-04-25 20:41:33

+1

@ Calibre2010:不行,這裏不需要另一個循環。你唯一需要做的就是改變start和/或endDate變量。 – Alex 2010-04-25 20:52:10

+0

如果我添加一個變量的年份值出錯了,因爲它不能看到一年的增加,只是在每月的價值增加? – Calibre2010 2010-04-26 09:58:56

1
public static string TimeLine(this HtmlHelper helper, string myString1) 
    { 
     StringBuilder string2 = new StringBuilder(); 

     DateTime startDate = new DateTime(2010, 1, 1); 
     DateTime endDate = new DateTime(2011, 12, 12); 

     string2.Append("<table>"); 

     for (DateTime date = startDate; date <= endDate; date = date.AddMonths(1)) 
     { 



      string2.Append("<p>" + date.AddMonths(1) + "</p>"); 

      //DateTime ddd = new DateTime(year, month); 

      string2.Append("<tr>"); 

      for (date = startDate; date <= endDate; date = date.AddMonths(1).AddDays(1)) 
      { 
       DayOfWeek dw = date.DayOfWeek; 

       var dateShortHand = ""; 

       switch (dw) 
       { 
        case DayOfWeek.Monday: 
         dateShortHand = "M"; 
         break; 
        case DayOfWeek.Tuesday: 
         dateShortHand = "T"; 
         break; 
        case DayOfWeek.Wednesday: 
         dateShortHand = "W"; 
         break; 
        case DayOfWeek.Thursday: 
         dateShortHand = "T"; 
         break; 
        case DayOfWeek.Friday: 
         dateShortHand = "F"; 
         break; 
        case DayOfWeek.Saturday: 
         dateShortHand = "S"; 
         break; 
        case DayOfWeek.Sunday: 
         dateShortHand = "S"; 
         break; 
       } 
       string2.Append("<td>" + dateShortHand + "</td>"); 

      } 
      string2.Append("</tr>"); 





      string2.Append("<tr>"); 

      //for (int i = 1; i <= ff; date = date.AddDays(1)) 
      //{ 

      // var f = date.Day; 
      // string2.Append("<td>" + f + "</td>"); 

      //} 
      string2.Append("</tr>"); 



     } 



     string2.Append("</table>"); 


     return string2.ToString(); 

    } 

嗨,我不熟悉linq,雖然我嘗試過這樣做的替代方法,我仍然有一點麻煩。我認爲它與我如何編碼for循環。反正有沒有這樣做,否則使用LINQ?

相關問題