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("您輸入的格式不對");
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM