Preetam Ramdhave
Preetam Ramdhave

Reputation: 38

Convert HH:mm:ss,dd-MM-yyyy from string to date format

I am trying to convert a string of the below format HH:mm:ss,dd-MM-yyyy into DateTime value and i am not able to can you please help

GetDateTime = "16:30:52,11-14-2013"
  DateTime currentDatetime;
  DateTime.TryParseExact(GetDateTime, "HH:mm:ss,dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out currentDatetime);

and i am getting

1/1/0001 12:00:00 AM

can any one help !

Upvotes: 0

Views: 1469

Answers (3)

Amol Kadam
Amol Kadam

Reputation: 85

string dateString = "14/11/2013 16:30:52"; // <-- Valid
string format = "dd/MM/yyyy HH:mm:ss";
DateTime dateTime;
if (DateTime.TryParseExact(dateString,format,CultureInfo.InvariantCulture,DateTimeStyles.None, out dateTime))
{
Console.WriteLine(dateTime);
}

Upvotes: 1

Microsoft DN
Microsoft DN

Reputation: 10020

Either change to

GetDateTime = "16:30:52,14-11-2013";

OR

DateTime.TryParseExact(GetDateTime, "HH:mm:ss,MM-dd-yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out currentDatetime);

Upvotes: 2

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73442

Month is in wrong format. hence "Parse" fails.

Is there any month called 14? I think you mean MM-dd-yyyy?

Upvotes: 12

Related Questions