public static string GetCalendarHtml(int index = 0)
{
DateTime day1 = new DateTime(DateTime.Now.AddMonths(index).Year, DateTime.Now.AddMonths(index).Month, 1);
int week1 = (int)day1.DayOfWeek;//获取当年当月1号的星期
int lastday = day1.AddMonths(1).AddDays(-1).Day; //获取当月的最后一天
int lastdayofweek = (int)day1.AddMonths(1).AddDays(-1).DayOfWeek;//获取当月的最后一天的星期
System.Text.StringBuilder builder = new System.Text.StringBuilder();
builder.Append("<table>");
builder.Append("<tbody>");
builder.Append("<tr>");
builder.Append("<th>日</th>");
builder.Append("<th>一</th>");
builder.Append("<th>二</th>");
builder.Append("<th>三</th>");
builder.Append("<th>四</th>");
builder.Append("<th>五</th>");
builder.Append("<th>六</th>");
builder.Append("</tr>");
string emptyString = "<td> </td>";
if (week1 > 0)
{
builder.Append("<tr>");
for (int i = 0; i < week1; i++)//1号前方td
{
builder.Append(emptyString);
}
}
for (int i = 1; i <= lastday; i++)
{
string day = string.Format("{0:00} ", i);//按01 02 输出
builder.Append(string.Format("<td>{0}</td>", day));
if ((i + week1) % 7 == 0)
{
builder.Append("</tr><tr>");
}
}
for (int i = 1; i < (7 - lastdayofweek); i++)//补全剩余td(可根据选择是否补全)
{
builder.Append(emptyString);
}
builder.Append("</tr>");
builder.Append("</table>");
return builder.ToString();
}
C# 输出日历Html
最新推荐文章于 2024-08-29 16:57:08 发布