当前位置:首页 > Windows程序 > 正文

C#输出日历

2021-03-25 Windows程序

 

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表格输出:

[csharp] 

 

#region 生成表格日历  

      /// <summary>  

      /// 生成表格日历 index:月份偏量,用来查看上一月下一月  

      /// </summary>  

      /// <param name="index"></param>  

      /// <returns></returns>  

      public static string GetCalendarHtml(int index = 0)  

      {  

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/67511.html