public void dateList() { // 输入年份 Console.Write("请输入年份:"); int year = int.Parse(Console.ReadLine()); // 使用List集合来存储日历 var calendar = GenerateCalendar(year); calendar.ForEach(x => Console.WriteLine($"{x.year}年{x.month}月{x.day}日{x.WeekDay}星期")); } public static List<date> GenerateCalendar(int year) { List<date> ymrList = new List<date>(); // 循环生成每个月的日历 for (int month = 1; month <= 12; month++) { // 获取指定年份和月份的第一天 DateTime firstDayOfMonth = new DateTime(year, month, 1); // 计算第一天是星期几 int firstDayOfWeek = (int)firstDayOfMonth.DayOfWeek; // 获取指定年份和月份的总天数 int totalDaysOfMonth = DateTime.DaysInMonth(year, month); for (int i = 1; i <= totalDaysOfMonth;) { if (firstDayOfWeek > 6) { firstDayOfWeek = 0; } ymrList.Add(new date { year = year, month = month, day = i++, WeekDay = firstDayOfWeek++ }); } } return ymrList; } public class date { public int year { get; set; } public int month { get; set; } public int day { get; set; } public int WeekDay { get; set; } }
标签:set,int,天数,List,month,获取,year,public From: https://www.cnblogs.com/zzz7/p/17503482.html