如何用函数去计算x年x月x日是?
由于现在人工智能的普及,我们往往会用计算机去算,或者去记录事情
1.计算某一年某一个月有多少天
2.计算某年某月某日是周几
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 程序日
{
internal class Program
{
static void Main(string[] args)
{
//
Console.WriteLine(DateTime.Now.DayOfWeek);//这个是计算今天周几
Console.WriteLine(DateTime.Now.DayOfYear);//这个是计算这个月是几号
//计算某年某月有多少天
Console.WriteLine("请输入年份");
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入月份");
int month = Convert.ToInt32(Console.ReadLine());
int day = DateTime.DaysInMonth(year, month);
Console.WriteLine($"{year}年{month}月,有{day}天");
//2.计算某年某月某日是周几
Console.WriteLine("请输入年份");
int y=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入月份");
int m=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入日份");
int d=Convert.ToInt32(Console.ReadLine());
if (m == 1 || m == 2) { m += 12; y--; }
if (m == 1 || m == 2) { m += 12; y--; }
int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7;
string weekstring = "";
switch (week)
{
case 0: weekstring = "星期日"; break;
case 1: weekstring = "星期一"; break;
case 2: weekstring = "星期二"; break;
case 3: weekstring = "星期三"; break;
case 4: weekstring = "星期四"; break;
case 5: weekstring = "星期五"; break;
case 6: weekstring = "星期六"; break;
default: break;
}
Console.WriteLine("2007 年 10 月 1 日是" + weekstring);
}
}
}