cusimar9
cusimar9

Reputation: 5259

Convert Date String to DateTime

I have to convert a date string to a DateTime object as follows

tmpdate = "27-Apr 14:53";
TheDate = DateTime.ParseExact(tmpdate, "DD-MMM HH:mm", CultureInfo.InvariantCulture);  

I keep getting exceptions about the string not being a valid date time. I've tried adding in the year as well with no luck. Any suggestions?

Upvotes: 1

Views: 1448

Answers (3)

Rob P.
Rob P.

Reputation: 15071

string tmpdate = "27-Apr 14:53";
DateTime TheDate = DateTime.ParseExact(tmpdate, "dd-MMM HH:mm", System.Globalization.CultureInfo.InvariantCulture); 

Upvotes: 1

alexl
alexl

Reputation: 6851

try with little d :

TheDate = DateTime.ParseExact(tmpdate, "dd-MMM HH:mm", CultureInfo.InvariantCulture);

Upvotes: 1

Anton Gogolev
Anton Gogolev

Reputation: 115691

Try dd-MMM HH:mm - note lowercased dd.

Upvotes: 5

Related Questions