ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#学习-打印年历,使用datetime

2021-08-26 20:01:17  阅读:171  来源: 互联网

标签:DateTime Console C# month int static year 年历 datetime


 1 /*编写日期2021/8/16
 2 **开发者:市井道士
 3 **为方便同样在此学习的各位同学,在此放出自己的答案供大家参考学习,仅供参考,随意转载*/
 4 using System;
 5 
 6 namespace d03
 7 {
 8 class Program
 9 {
10 static void Main(string[] args)
11 {
12 Calendar calendar = new Calendar();
13 calendar.Printdate(DateTime.Now.Year);
14 Console.WriteLine("编写日期2021/8/16");
15 Console.WriteLine("开发者:市井道士");
16 Console.WriteLine("为方便同样在此学习的各位同学,在此放出自己的答案供大家参考学习,仅供参考,随意转载");
17 Console.ReadLine();
18 }
19 }
20 class Calendar
21 {
22 /// <summary>
23 /// 输入年月日,得到这天是星期几
24 /// </summary>
25 /// <param name="year">年</param>
26 /// <param name="month">月</param>
27 /// <param name="day">日</param>
28 /// <returns>星期几</returns>
29 private static int GetWeekByDay(int year, int month, int day)
30 {
31 DateTime dt = new DateTime(year, month, day);
32 return (int)dt.DayOfWeek;
33 }
34 
35 /// <summary>
36 /// 获取某个月的天数,输入(int)年份,月份,返回天数(int)
37 /// </summary>
38 /// <param name="year">年</param>
39 /// <param name="month">月</param>
40 /// <returns>天数</returns>
41 private static int GetMonthDay(int year, int month)
42 {
43 int thismonthdays = DateTime.DaysInMonth(year, month);
44 return thismonthdays;
45 }
46 /// <summary>
47 /// 打印年历
48 /// </summary>
49 /// <param name="year"></param>
50 public void Printdate(int year)
51 {
52 int nextlinecount;//使用一个计数器没过一天就加1,逢7换行
53 for (int month = 1; month <= 12; month++)
54 {
55 nextlinecount = 0;//计数器每个月开始需要进行初始化
56 Console.WriteLine("{0}年{1}月",year, month);
57 Console.WriteLine("星期天\t 星期一\t 星期二\t 星期三\t 星期四\t 星期五\t 星期六\t");
58 //获取每个月第一天是星期几然后输出对应次数的空格
59 for (int count=1;count <= GetWeekByDay(year,month,1);count++ ){
60 Console.Write(" \t ");
61 nextlinecount++;//计数器增加,这里的空的是上个月的日子
62 }
63 for (int day = 1; day <= GetMonthDay(year, month); day++)
64 {
65 if (nextlinecount % 7 == 0)//每次打印日期前先判断是否为周六,逢7换行
66 Console.WriteLine();
67 Console.Write(day+"\t ");
68 nextlinecount++;
69 }
70 Console.WriteLine();
71 Console.WriteLine();
72 Console.WriteLine("=========================================================================");
73 Console.WriteLine();
74 }
75 }
76 }
77 }

 

标签:DateTime,Console,C#,month,int,static,year,年历,datetime
来源: https://www.cnblogs.com/tangjiarun/p/15150546.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有