1 public string GetDayList(DateTime? date) 2 { 3 int day = 0; 4 var minDate = Convert.ToDateTime(((DateTime)(date ?? DateTime.Now)).ToString("yyyy-MM-01")); 5 var maxDate = minDate.AddMonths(1).AddDays(-1); 6 7 var sb = new StringBuilder(); 8 9 sb.Append("<table>"); 10 sb.Append("<thead>"); 11 sb.Append("<tr>"); 12 sb.Append("<th>周日</th><th>周一</th><th>周二</th><th>周三</th><th>周四</th><th>周五</th><th>周六</th>"); 13 sb.Append("</tr>"); 14 sb.Append("</thead>"); 15 sb.Append("<tbody>"); 16 sb.Append("<tr>"); 17 while (true) 18 { 19 if (minDate > maxDate) 20 { 21 if (day > 6) 22 { 23 break; 24 } 25 else 26 { 27 sb.Append("<td></td>"); 28 day++; 29 continue; 30 } 31 } 32 33 if ((int)minDate.DayOfWeek == day) 34 { 35 sb.AppendFormat("<td>{0}</td>", minDate.Day); 36 minDate = minDate.AddDays(1); 37 } 38 else 39 { 40 sb.Append("<td></td>"); 41 } 42 43 if (day == 6) 44 { 45 sb.Append("</tr><tr>"); 46 day = -1; 47 } 48 49 day++; 50 } 51 sb.Append("</tr>"); 52 sb.Append("</tbody>"); 53 sb.Append("</table>"); 54 55 return sb.ToString(); 56 }
加上CSS后效果图: