Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friend,

i need a help to solve this problem.I am trying to convert a string [4/20/2011 8:17:21 AM] to DateTime. For this i tried like the following ways..

string date = "4/20/2011 8:17";

Convert.ToDateTime(date);

and

DateTime.ParseExact(date,"M/dd/yyy",System.Globalization.CultureInfo.CurrentCulture);

But i am getting error message "String was not recognized as a valid DateTime."

Please help me ..
Posted

For the Convert.ToDateTime call, it's okay (or should be). For the ParseExact call, your format string needs to be this:

"M/dd/yyyy h:mm"
 
Share this answer
 
v2
try this,

string strDateTime = "2/28/1992 12:15:12";
DateTime myDateTime = DateTime.Parse(strDateTime );

Or

myDate = DateTime.ParseExact("2009-05-08 14:40:52,531", "yyyy-MM-dd HH:mm:ss,fff",System.Globalization.CultureInfo.InvariantCulture)
 
Share this answer
 
v2
try with this using System.Globalization

C++
CultureInfo provider = CultureInfo.InvariantCulture;
            string date = "04/20/2011 08:17";
            DateTime dt1 = DateTime.ParseExact(date, "g",provider);
 
Share this answer
 
string d="04/20/2011";
datetime d1=convert.todatetime(d);

try this
 
Share this answer
 
v2

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