1 text format
/// <summary>
/// Lesson 12 of "Xiaobai Learning Program": Calendar preparation, time DateTime calculation method and code
/// This lesson learns the simple method of time type data DateTime, and compiles a month calendar.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button12_Click(object sender, EventArgs e)
{ // Set as the 1st of a certain month DateTime day = DateTime.Parse("2025-08-01 10:10:00"); // Record the month int month = day.Month; // What day is the 1st? Equal to the number of spaces left before the 1st day of the calendar; DayOfWeek weekday = day.DayOfWeek; int wd = (int)weekday;
StringBuilder sb = new StringBuilder();
sb.AppendLine("<style>td { padding:5px;text-align:center;} </style>");
sb.AppendLine("<table width=420 border=1 style='border-collapse:collapse;'>");
// The first row of the calendar, header, actual Sunday, Monday, Tuesday. . . . . .
sb.AppendLine("<tr style='background-color:#EEEEFF;'>");
sb.AppendLine("<td>day</td>");
sb.AppendLine("<td>one</td >");
sb.AppendLine("<td>two</td>");
sb.AppendLine("<td>three</td>");
sb.AppendLine("<td>four</td>" );
sb.AppendLine("<td>five</td>");
sb.AppendLine("<td>six</td>");
sb.AppendLine("</tr>");
sb.AppendLine("<tr>");
// If the 1st is not a Sunday, leave some spaces in front
if (wd > 0)
{ int nn = wd; for (int i = 0; i < nn; i++) { sb. AppendLine("<td> </td>"); } }
// loop, the condition is that the month does not change!
while (day.Month == month)
{ // Print the date sb.AppendLine("<td>" + day.Day + "</td>"); // If it is Sunday, wrap! ! ! if ((int)day.DayOfWeek == 6) { sb.AppendLine("</tr>"); sb.AppendLine("<tr>"); } // add one day to the time day = day.AddDays( 1); }
// The last day, if it is not Saturday, need to add a few spaces!
int de = (int)day.DayOfWeek;
for (int i = de; i < 6; i++)
{ sb.AppendLine("<td> </td>"); }
sb.AppendLine("</tr>");
sb.AppendLine("</table>");
webBrowser1.DocumentText = sb.ToString();
}
2 code format
/// <summary>
/// 《小白学程序》第十二课:日历的编制,时间DateTime的计算方法与代码
/// 本课学习时间类型的数据 DateTime 的简单方法,并编制一个月的日历。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button12_Click(object sender, EventArgs e)
{
// 设定为某个月的1号
DateTime day = DateTime.Parse("2025-08-01 10:10:00");
// 记录 月份
int month = day.Month;
// 1号 是星期几? 等于日历1号前面留出的空格数;
DayOfWeek weekday = day.DayOfWeek;
int wd = (int)weekday;
StringBuilder sb = new StringBuilder();
sb.AppendLine("<style>td { padding:5px;text-align:center;} </style>");
sb.AppendLine("<table width=420 border=1 style='border-collapse:collapse;'>");
// 日历第一行,表头,现实星期日、一、二、。。。。。。
sb.AppendLine("<tr style='background-color:#EEEEFF;'>");
sb.AppendLine("<td>日</td>");
sb.AppendLine("<td>一</td>");
sb.AppendLine("<td>二</td>");
sb.AppendLine("<td>三</td>");
sb.AppendLine("<td>四</td>");
sb.AppendLine("<td>五</td>");
sb.AppendLine("<td>六</td>");
sb.AppendLine("</tr>");
sb.AppendLine("<tr>");
// 如果 1号不是周日,前面留出一些空格
if (wd > 0)
{
int nn = wd;
for (int i = 0; i < nn; i++)
{
sb.AppendLine("<td> </td>");
}
}
// 循环,条件是 月份 不变!
while (day.Month == month)
{
// 打印日期
sb.AppendLine("<td>" + day.Day + "</td>");
// 如果是周日,换行!!!
if ((int)day.DayOfWeek == 6)
{
sb.AppendLine("</tr>");
sb.AppendLine("<tr>");
}
// 时间往后面加一天
day = day.AddDays(1);
}
// 最后一天,如果不是周六,需要增加几个空格!
int de = (int)day.DayOfWeek;
for (int i = de; i < 6; i++)
{
sb.AppendLine("<td> </td>");
}
sb.AppendLine("</tr>");
sb.AppendLine("</table>");
webBrowser1.DocumentText = sb.ToString();
}