Solved

ASP.NET: Check date input

Posted on 2008-10-07
6
887 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now