How to convert date from yyyyMMdd format to mm-dd-yyyy format

PramodChoudhari picture PramodChoudhari · Jan 17, 2011 · Viewed 58.1k times · Source

I want to convert a string which contains the date in yyyyMMdd format to mm-dd-yyyy DateTime format. How do I get it?

Answer

Daniel Elliott picture Daniel Elliott · Jan 17, 2011
var dateString = "20050802";
var date = myDate = DateTime.ParseExact(dateString,
                                    "yyyyMMdd",
                                    System.Globalization.CultureInfo.InvariantCulture);