C# learning – print calendar, use datetime

Time:2022-5-14
1 / * prepared on August 16, 2021
 2 * * developer: marketplace Taoist
 3 * * for the convenience of all students who also study here, we release our own answers here for your reference and study. They are only for reference and can be reproduced at will*/
 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 ("written on August 16, 2021");
15 Console. Writeline ("developer: marketplace Taoist");
16 Console. Writeline ("for the convenience of all students who also study here, we release our own answers here for your reference and study, only for reference and reprint at will");
17 Console.ReadLine();
18 }
19 }
20 class Calendar
21 {
22 /// 
23 // enter the month, year and day to get the day of the week
24 /// 
25 // years
26 // month
27 // day
28 // day of the week
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 /// 
36 // get the number of days of a month, enter (int) year and month, and return the number of days (int)
37 /// 
38 // years
39 // month
40 // days
41 private static int GetMonthDay(int year, int month)
42 {
43 int thismonthdays = DateTime.DaysInMonth(year, month);
44 return thismonthdays;
45 }
46 /// 
47 // print calendar
48 /// 
49 /// 
50 public void Printdate(int year)
51 {
52 int nextlinecount;// Use a counter to add 1 in less than a day and wrap every 7
53 for (int month = 1; month <= 12; month++)
54 {
55 nextlinecount = 0;// The counter needs to be initialized at the beginning of each month
56 Console. Writeline ("{0} year {1}", year, month);
57 Console. Thursdays (Thursdays);
58 // get the day of the week on the first day of each month, and then output the corresponding number of spaces
59 for (int count=1;count <= GetWeekByDay(year,month,1);count++ ){
60 Console.Write(" \t ");
61 nextlinecount++;// The counter increases, and the empty here is the day of last month
62 }
63 for (int day = 1; day <= GetMonthDay(year, month); day++)
64 {
65 if (nextlinecount% 7 = = 0) // judge whether it is Saturday before printing the date every time, and wrap the line every 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 }

 

Recommended Today

Extcon driver and its application in USB driver

Extcon, short for external connector, is used to abstract external connectors, such as audio jack, USB microb / typec interface, etc. Its prototype is the switch class driver of Android. After modification, it was introduced into the kernel in kernel version 3.4.0. Extcon (external connector): import Android’s switch class and modify. External connector class (extcon) is based on and an extension of Android kernel’s switch class located at linux/drivers/switch/. https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/drivers/extcon?h=next-20220502&id=de55d8716ac50a356cea736c29bb7db5ac3d0190 The main function of extcon driver is to identify the […]