【C#每日一题】输入任意一个日期显示出它是当年的第几天?星期几?并打印出当月的日历

在这里插入图片描述

作业1:输入任意一个日期显示出它是当年的第几天?星期几?并打印出当月的日历

运行结果:
在这里插入图片描述

上代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
           // string aa= "2021-05-24";
            DateTime sj = new DateTime(2022,05,24);
            int days = (int)sj.DayOfYear;
            int week = (int)sj.DayOfWeek;
            int month = (int)sj.Month;
            int year = (int)sj.Year;
            Console.Write("该日期是当年中的:" + days + "天\n");
            Console.Write("星期:" + week + "\n");
            int last = (int)DateTime.DaysInMonth(year, month);
           // Console.Write(last);
            //31天
            Console.Write("星期日 星期一 星期二 星期三 星期四 星期五 星期六\n");
         
            for (int i = 1; i <= last; i++)
            {
    
    

                if (i % 7 == 0)
                {
    
    
                    Console.Write(" {0:00}  \n", i);
                }
                else {
    
    
                    Console.Write("   {0:00}  ", i);
                }
            }
            Console.ReadKey();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_35230125/article/details/124953999
今日推荐