Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
String was not recognized as a valid DateTime. in C#
my datesting is

lblStartDate.Text=4/16/2014 12:00:00:AM;


i tried this

C#
DateTime dtstring1 = DateTime.ParseExact(lblStartDate.Text, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
dtStartDate = Convert.ToDateTime(dtstring1.ToString("dd/MM/yyyy"));            


Suggest me to get a solution

Thanks in advance.
Posted

Quote:
lblStartDate.Text=4/16/2014 12:00:00:AM;

There's a spurious colon (':') before the AM designator.
 
Share this answer
 
Why?
Why are you converting a user string to a DateTime, then converting that to a string, in order to convert it right back to a DateTime again?

The error you are getting is probably in the second line, because you are specifying the exact format of the string you are converting the DateTime to, but leaving the ToDateTime conversion to use teh standard system wide date format - so if they don't match you will get an error.

What it looks like you are trying to do is read a DateTime from the user, then throw away the time part - which doesn't need any string conversions at all:
C#
DateTime dtstring1 = DateTime.ParseExact(lblStartDate.Text, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
dtStartDate = dtstring1.Today;
Will do it.
 
Share this answer
 
Comments
[no name] 20-May-14 3:34am    
No iam getting error in 1st line.You don't mind second line it is for further use.but in the 1st line only for conversion
OriginalGriff 20-May-14 4:11am    
Then try removing the ":" from your input string, or include it in the ParseExact:

lblStartDate.Text="4/16/2014 12:00:00 AM";
DateTime dtstring1 = DateTime.ParseExact(lblStartDate.Text, "M/d/yyyy h:mm:ss tt", ...

Or

lblStartDate.Text="4/16/2014 12:00:00:AM";
DateTime dtstring1 = DateTime.ParseExact(lblStartDate.Text, "M/d/yyyy h:mm:ss:tt", ...
try this

DateTime varDate;
varDate.ToString("yyyy-MM-dd h:mm:ss tt")


ToString, one can specify the date pattern.
 
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
Top Experts
Last 24hrsThis month


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