Solved

ASP.NET: Check date input

Posted on 2008-10-07
6
886 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

18 Experts available now in Live!

Get 1:1 Help Now