Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

ASP.NET: Check date input

Posted on 2008-10-07
6
889 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:karakav
6 Comments
 
LVL 5

Expert Comment

by:Didier Vally
ID: 22657240
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

0
 
LVL 6

Expert Comment

by:avnish_tanna
ID: 22657528
check if below link helps:
http://www.daniweb.com/forums/thread34168.html (see post #7)
0
 
LVL 4

Author Comment

by:karakav
ID: 22657534
I won't use a compare validator to compare values in a file.?!?!?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 14

Expert Comment

by:Binuth
ID: 22657689
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

0
 
LVL 4

Author Comment

by:karakav
ID: 22657887
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

0
 
LVL 3

Accepted Solution

by:
trunghieubkit earned 500 total points
ID: 22657944
Your result does not depend on the 2 lines

CultureInfo cult = new CultureInfo("en-US");
DateTimeStyles stl = DateTimeStyles.None;

"yyyyMMdd" is Japan culture,




DateTime entryD;
CultureInfo cult = CultureInfo.InvariantCulture;
DateTimeStyles stl = DateTimeStyles.AssumeLocal;
if (!DateTime.TryParseExact("20070122", "yyyyMMdd", cult, stl, out entryD))
{
    return;
}

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question