控制台输出对应年份
using System; using System.Text; namespace ConsoleApp1 { class Program { static void Main(string[] args) { RiLi(); Console.ReadKey(); } /// <summary> /// 万年历 /// </summary> static void RiLi() { Console.WriteLine("请输入年份:"); int year = Convert.ToInt32(Console.ReadLine()); while (year < 1900) { Console.WriteLine("请输入大于1900年的年份!"); year = Convert.ToInt32(Console.ReadLine()); } for (int i = 1; i < 13; i++) { //得到指定的年份和月它的天数 int days = DateTime.DaysInMonth(year, i); //得到当前月的第一天 DateTime currday = new DateTime(year, i, 1); //得到下月第一天 DateTime lastday = currday.AddMonths(1); Console.WriteLine(); Console.WriteLine("{0}年{1}月", year, i); Console.WriteLine("周日\t周一\t周二\t周三\t周四\t周五\t周六"); while (currday < lastday) { //如果日期为当月第一天 if (currday.Day == 1) { //获取当前日期是星期几 int weekday = Convert.ToInt32(currday.DayOfWeek); //用于拼接字符串 StringBuilder sb = new StringBuilder(); for (int j = 0; j < weekday; j++) { sb.Append("\t"); } Console.Write("{0}{1}", sb.ToString(), currday.Day); } else if (currday.DayOfWeek == DayOfWeek.Sunday) //如果当前日期是礼拜天 { Console.WriteLine(); Console.Write("{0}", currday.Day); } else { Console.Write("\t{0}", currday.Day); } currday = currday.AddDays(1);//明天 } } } } }