Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do you convert a string suchas "04-13-13 05:25PM" into a datetime?
Posted

 
Share this answer
 
C#
System.Globalization.DateTimeFormatInfo myFormat = new System.Globalization.DateTimeFormatInfo();
myFormat.DateSeparator = "-";
myFormat.TimeSeparator = ":";
myFormat.AMDesignator = "AM";
myFormat.PMDesignator = "PM";

DateTime myDate = DateTime.MinValue;
string data = "04-13-13 05:25PM";

if (DateTime.TryParseExact(data, "MM-dd-yy hh:mmtt", myFormat, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out myDate))
{
    // Correct
}
else
{
    // Wrong
}

return myDate;
 
Share this answer
 
v2
Comments
Oshtri Deka 2-May-13 8:59am    
Long, but correct answer. ;)
Mamta Yadav 3-May-13 1:14am    
Thank you for providing solution.Its working...:)
Try this
C#
string date = "04-13-13 05:25PM";
DateTime dt = Convert.ToDateTime(date);
 
Share this answer
 
v2
Comments
adriancs 2-May-13 9:07am    
OP want to convert a string to datetime.
KM Perumal 2-May-13 9:12am    
Now try this
u have to convert

SELECT CONVERT(VARCHAR(8), '04-13-13 05:25PM', 2) AS [YY.MM.DD]


u will get output:04-13-13 (YY-MM-DD)
 
Share this answer
 
Comments
Oshtri Deka 2-May-13 8:58am    
OP tagged this question as C#.

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



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