Math9 Math9 - 12 days ago 10
ASP.NET (C#) Question

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 ?

Answer

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);