Math9
Math9

Reputation: 99

Convert dd/MM/yyyy hh:mm am/pm to MM/dd/yyyy hh:mm am/pm

I want to convert a string : 24/11/2016 04:30 pm to datetime value : 11/24/2016 04:30 pm.

My code as :

DateTime date = DateTime.ParseExact("24/11/2016 04:30 pm", "dd/MM/yyyy hh:mm aa", CultureInfo.InvariantCulture);

But I get a error :

String was not recognized as a valid DateTime.

What can I do ?

Upvotes: 3

Views: 20286

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460138

For the AM/PM designator you have to use tt not aa. Reading:

DateTime date = DateTime.ParseExact("24/11/2016 04:30 pm", "dd/MM/yyyy hh:mm tt", CultureInfo.InvariantCulture);

Upvotes: 14

Related Questions