当前位置:首页 > 通信资讯 > 正文

C#输出日历(输出日历c语言)

本文实例讲述了C#简单输出日历的方法。分享给大家供大家参考。具体如下:

用C#输出日历,此功能可用于Ajax方式列出计划日程相关的内容,由于是C#控制输出,可以方便加上自己需要的业务处理逻辑。

1.控制台输出:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 using System; namespace 控制台日历 { class Program { public static void Main(string[] args) { string s = " "; Console.WriteLine("输入年份:"); int nYear = int.Parse(Console.ReadLine()); Console.WriteLine("输入月份:"); int nMonth = int.Parse(Console.ReadLine()); DateTime day1 = new DateTime(nYear,nMonth,1); Console.WriteLine("{0}/{1}",day1.Year,day1.Month); Console.WriteLine("日 一 二 三 四 五 六"); int week1 =(int )day1.DayOfWeek;//获取当年当月1号的星期 //Console.WriteLine("当月一号的星期{0}",week1); int lastday = day1.AddMonths(1).AddDays(-1).Day; //获取当月的最后一天 for (int i = 0; i < week1; i++) Console.Write(s);//不能换行输出 for (int i = 1; i <= lastday; i++) { Console.Write("{0:00} ", i);//按01 02 输出 if ((i + week1) % 7 == 0) Console.WriteLine(); } Console.WriteLine(); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } }

效果图:

C#输出日历(输出日历c语言)

2.Html表格输出:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #region 生成表格日历 /// <summary> /// 生成表格日历 index:月份偏量,用来查看上一月下一月 /// </summary> /// <param name="index"></param> /// <returns></returns> 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; //获取当月的最后一天 System.Text.StringBuilder builder = new System.Text.StringBuilder(); builder.Append(string.Format("<table class='calendar_table'><caption><span style='cursor:pointer' class='prevMonth' onclick='javascript:changeMonth(-1)'>上一月</span><span class='currMonth'> {0}年{1}月</span><span style='cursor:pointer' class='nextMonth' onclick='javascript:changeMonth(1)'>下一月</span></caption>", DateTime.Now.AddMonths(index).Year, DateTime.Now.AddMonths(index).Month)); builder.Append("<tr class='calendar_head'>"); builder.Append("<td class='calendar_cell'>日</td>"); builder.Append("<td class='calendar_cell'>一</td>"); builder.Append("<td class='calendar_cell'>二</td>"); builder.Append("<td class='calendar_cell'>三</td>"); builder.Append("<td class='calendar_cell'>四</td>"); builder.Append("<td class='calendar_cell'>五</td>"); builder.Append("<td class='calendar_cell'>六</td>"); builder.Append("</tr>"); string emptyString = "<td class='calendar_cell'> </td>"; if (week1 > 0) { builder.Append("<tr class='calendar_body'>"); for (int i = 0; i < week1; i++) { builder.Append(emptyString); } } for (int i = 1; i <= lastday; i++) { string day = string.Format("{0:00} ", i);//按01 02 输出 builder.Append(string.Format("<td class='calendar_cell'>{0}</td>", day)); if ((i + week1) % 7 == 0) { builder.Append("</tr><tr class='calendar_body'>"); } } builder.Append("</tr>"); builder.Append("</table>"); return builder.ToString(); } #endregion

希望本文所述对大家的C#程序设计有所帮助。

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。