Best way to get a date in .NET?
Posted
by
frenchie
on Stack Overflow
See other posts from Stack Overflow
or by frenchie
Published on 2011-02-10T20:28:02Z
Indexed on
2011/02/11
23:25 UTC
Read the original article
Hit count: 169
I'm getting a string back from my page and I want to make sure it's a date. This is what I have so far (it works) and I just want to know if this is the "best" way to do it. I'm using .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; }
}
© Stack Overflow or respective owner