Problem with datetime.ParseExact()

string strDate = "04/23/2019";
string[] arr = {"MM/dd/yyyy","M/d/yyyy"};
DateTime dt = DateTime.ParseExact(strDate, arr, new CultureInfo("en-US"),
                                         DateTimeStyles.None);

Console.WriteLine(dt.ToShortDateString());

The sample C# code works for me.

You must initialize the culture info object. Then only the array of formats will be considered and works.

Regards,
Karthik Byggari

8 Likes