C#datetime判断日期输入是否正确

//7.输入年月日,看看格式是否正确。利用DateTime。
//(1)
//DateTime dt=DateTime.Now;
//Console.Write("请输入现在的年:");
//string n = Console.ReadLine();
//Console.Write("请输入今天是几月:");
//string y = Console.ReadLine();
//Console.Write("请输入今天几号:");
//string r = Console.ReadLine();
//string m=n+"年"+y+"月"+r+"日";
//if (m == dt.ToLongDateString().ToString())
//{
// Console.WriteLine("您输入的日期正确是今天");
// Console.WriteLine("今天的日期是:" + dt.ToLongDateString().ToString());
//}
//else
//{
// Console.WriteLine("您输入的日期不正确不是今天");
// Console.WriteLine("今天的日期是:" + dt.ToLongDateString().ToString());
//}
//(2)

DateTime dt;
try
{
Console.Write("请输入日期:(例如:2000-01-01 或 2000/01/01):");
dt = DateTime.Parse(Console.ReadLine());
Console.WriteLine("您输入的格式正确,日期为:{0}年{1}月{2}日", dt.Year, dt.Month, dt.Day);
}
catch
{
Console.WriteLine("您输入的格式不对");
}

原文地址:https://www.cnblogs.com/xiongxiaobai/p/5282906.html