Parsing Date strings in "Short Date" format

I had to solve an interesting problem today. I have code that is reporting the expiration date of an Certificate, using the X509Certificate.GetExpirationDateString() method. This method "returns a string that shows the date formatted in Short Date Pattern followed by the time formatted in Long Time Pattern. The date and time are formatted using the current culture and time zone."  While this is nice, what if the value is being viewed in regions of the world with different date patterns?

By the way, App Services in Azure seem to use the en-us culture regardless of the region. I see confirmation on Stack Overflow, but couldn't find official docs on that. (Observationally correct, as a former colleague of mine would say. 🙂)

The solution – use the CurrentCulture to get a DateTimeFormat object, and parse the string using its properties:

var expDate = cert.GetExpirationDateString();

// expDate = "12/5/2021 6:53:59 PM"

var dtf = CultureInfo.CurrentCulture.DateTimeFormat;
var pattern = $"{dtfi.ShortDatePattern} {dtfi.LongTimePattern}";
var utcDate = DateTime.ParseExact(expDate, pattern, CultureInfo.InvariantCulture)

// utcDate = "2021-12-05 18:53:59Z"