C#输出日历

简介:

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

1.控制台输出:

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);
		}
	}
}
效果图:


2.Html表格输出:

  #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
页面前台结合javascript实现ajax日历切换效果,只需用js改变函数中的index偏移量即可。

目录
相关文章
|
小程序 定位技术 C#
C#编程学习(01):北斗时转日历时的小程序
C#编程学习(01):北斗时转日历时的小程序
C#编程学习(01):北斗时转日历时的小程序
【C#每日一题】输入任意一个日期显示出它是当年的第几天?星期几?并打印出当月的日历
作业1:输入任意一个日期显示出它是当年的第几天?星期几?并打印出当月的日历 运行结果: 上代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { .
191 0
【C#每日一题】输入任意一个日期显示出它是当年的第几天?星期几?并打印出当月的日历
|
API C# 开发工具
通过 C# 代码操作 Google 日历
原文:通过 C# 代码操作 Google 日历 本文主题 借助 Google .NET APIs Client Library,通过 C# 代码在 Google 日历中创建会议邀请。 本文背景 最近,Google 发布了 .NET APIs Client Library,终于可以方便地通过 .NET/C# 代码调用 Google 的 API。
1141 0
|
4月前
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
75 3
|
3月前
|
存储 安全 编译器
学懂C#编程:属性(Property)的概念定义及使用详解
通过深入理解和使用C#的属性,可以编写更清晰、简洁和高效的代码,为开发高质量的应用程序奠定基础。
141 12
|
4月前
|
设计模式 C# 图形学
Unity 游戏引擎 C# 编程:一分钟浅谈
本文介绍了在 Unity 游戏开发中使用 C# 的基础知识和常见问题。从 `MonoBehavior` 类的基础用法,到变量和属性的管理,再到空引用异常、资源管理和性能优化等常见问题的解决方法。文章还探讨了单例模式、事件系统和数据持久化等高级话题,旨在帮助开发者避免常见错误,提升游戏开发效率。
176 4