ASP.NET生成日历

时间:2021-02-11 14:45:53
 public string GetDayList(DateTime? date)
{
int day = ;
var minDate = Convert.ToDateTime(((DateTime)(date ?? DateTime.Now)).ToString("yyyy-MM-01"));
var maxDate = minDate.AddMonths().AddDays(-); var sb = new StringBuilder(); sb.Append("<table>");
sb.Append("<thead>");
sb.Append("<tr>");
sb.Append("<th>周日</th><th>周一</th><th>周二</th><th>周三</th><th>周四</th><th>周五</th><th>周六</th>");
sb.Append("</tr>");
sb.Append("</thead>");
sb.Append("<tbody>");
sb.Append("<tr>");
while (true)
{
if (minDate > maxDate)
{
if (day > )
{
break;
}
else
{
sb.Append("<td></td>");
day++;
continue;
}
} if ((int)minDate.DayOfWeek == day)
{
sb.AppendFormat("<td>{0}</td>", minDate.Day);
minDate = minDate.AddDays();
}
else
{
sb.Append("<td></td>");
} if (day == )
{
sb.Append("</tr><tr>");
day = -;
} day++;
}
sb.Append("</tr>");
sb.Append("</tbody>");
sb.Append("</table>"); return sb.ToString();
}

加上CSS后效果图:

ASP.NET生成日历