karakav
asked on
ASP.NET: Check date input
I am importing data from a excel file and I have to check the date format in one column. I use the code in the snippet but it does not work. How can I correct that?
//I want to check if the date is in format 'yyyymmdd'
DateTime entryD
CultureInfo cult = new CultureInfo("en-US");
DateTimeStyles stl = DateTimeStyles.AssumeLocal;
if (!DateTime.TryParse(row["EntryDate"].ToString(),cult, stl, out entryD)) //This always pass, even if the date is in ddmmyyyy format
{
//Do other processing here
}
check if below link helps:
http://www.daniweb.com/forums/thread34168.html (see post #7)
http://www.daniweb.com/forums/thread34168.html (see post #7)
ASKER
I won't use a compare validator to compare values in a file.?!?!?
use DateTime.ParseExact
try
{
// it will throw expection if string format is not yyyyMMdd
DateTime entryD = DateTime.ParseExact(row["EntryDate"].ToString(),"yyyyMMdd",null);
}
catch (Exception)
{
throw;
}
ASKER
I finally managed to do it this way:
//I want to check if the date is in format 'yyyymmdd'
DateTime entryD
CultureInfo cult = new CultureInfo("en-US");
DateTimeStyles stl = DateTimeStyles.None;
if (!DateTime.TryParseExact(row["EntryDate"].ToString(),"yyyyMMdd hh:mm:ss",cult, stl, out entryD)) //This always pass, even if the date is in ddmmyyyy format
{
//Do other processing here
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window