Solved

ASP.NET: Check date input

Posted on 2008-10-07
6
890 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
[X]
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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Selenium: Transitioning from unit testing to automated testing 4 62
Get sourcecode path 14 47
ModalPopup  question 22 39
c#, datatable, aspx 4 23
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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