Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I want to Convert string Formatted Date in to a DateTime Variable. But It Showing "String was not recognized as a valid DateTime." Plz HelpMe.

My String formatted date is 24/NOV/2013

DateTime dt = DateTime.ParseExact(txtDate.Text,@"dd/MMM/yyyy",System.Globalization.CultureInfo.InvariantCulture);

Thanks,
Bajid Khan
Posted
Comments
BillWoodruff 23-Nov-13 21:55pm    
If your string is "24/NOV/2013" ... your code is valid as it is. Something is else is wrong that you are missing.
Adam Zgagacz 23-Nov-13 22:05pm    
Hmm.. I tried code you posted and it works on my PC. Is there anything specific about you PC settings (language, date format etc.)?
Karthik_Mahalingam 23-Nov-13 22:26pm    
try this

DateTime dt = DateTime.ParseExact(txtDate.Text.Trim(),@"dd/MMM/yyyy",System.Globalization.CultureInfo.InvariantCulture);
Bajid Khan 23-Nov-13 22:38pm    
I tried Above code also... But Not Working... Showing same Eror.. "String was not recognized as a valid DateTime"
Karthik_Mahalingam 23-Nov-13 22:52pm    
can u post the txtDate.Text value...

At last i reproduce your problem. Space is the main issue there.
if you write the following code it will throw String was not recognize as valid format

C#
var date = " 24/NOV/2013";
            DateTime dt = DateTime.ParseExact(date, @"dd/MMM/yyyy", System.Globalization.CultureInfo.InvariantCulture);

when you put extra space in date field then the error is produced. So in your case that is happened. So you should use trim function before use ParseExtract method.

C#
DateTime dt = DateTime.ParseExact(txtDate.Text.Trim(),@"dd/MMM/yyyy",System.Globalization.CultureInfo.InvariantCulture);


Note if date string comes this form
var date = "24 / NOV /2013"//space inside date value
that time also throw the exception "string was not recognize as valid format"
in that case trim will not solve your problem. Then you should remove space from date value(inside date value). The following code sample i use linq with trim function for removing inside space.
C#
var date = txtDate.Text;//
            //var date = "24 / NOV /2013";
            date = String.Join("/", date.Split('/').Select(t => t.Trim()));
            DateTime dt = DateTime.ParseExact(date, @"dd/MMM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
 
Share this answer
 
v4
Comments
RaisKazi 23-Nov-13 22:44pm    
My 5! Good Answer.
S. M. Ahasan Habib 23-Nov-13 22:46pm    
thanks
Bajid Khan 23-Nov-13 23:21pm    
Thanq Very Much S.M. Ahasan Habib. I Got a solution from your Comments... Thanq once againn..
Thanks7872 23-Nov-13 22:49pm    
You would have gone through the comments. OP says that its not working even after removing white spaces.
S. M. Ahasan Habib 23-Nov-13 23:04pm    
thanks! i update my solution.
ALways use DateTime.TryParse to figure out whether the string is really a datetime or not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900