Link to home
Start Free TrialLog in
Avatar of karakav
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
}

Open in new window

Avatar of Didier Vx
Didier Vx
Flag of France image

You can use date validation in ASP.NET :

<asp:Label runat="server" AssociatedControlID="tbDate" Text="Date" /> <asp:TextBox ID="tbDate" runat="server" /> <asp:CompareValidator runat="server" ControlToValidate="tbDate" Text="*" ValidationGroup="MyValidationGroup" Operator="DataTypeCheck" Type="date" />

Open in new window

check if below link helps:
http://www.daniweb.com/forums/thread34168.html (see post #7)
Avatar of karakav
karakav

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;
	}

Open in new window

Avatar of karakav

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
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of trunghieubkit
trunghieubkit
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial