我从页面中获取了一个字符串,并希望确保它是一个日期。这是我到目前为止的代码(它可以工作),但我想知道这是否是最好的方法。我正在使用 .NET 4。
int TheMonth =0;
int TheDay = 0;
int TheYear = 0;
DateTime NewDate;
var TheIncomingParam = Request.Params.Get("__EVENTARGUMENT").ToString();
char[] TheBreak = { '/' };
string[] TheOutput = TheIncomingParam.Split(TheBreak);
try { TheMonth = Convert.ToInt32(TheOutput[0]); }
catch { }
try { TheDay = Convert.ToInt32(TheOutput[1]); }
catch { }
try { TheYear = Convert.ToInt32(TheOutput[2]); }
catch { }
if (TheMonth!=0 && TheDay!=0 && TheYear!=0)
{
try { NewDate = new DateTime(TheYear, TheMonth, TheDay); }
catch { var NoDate = true; }
}