Since a. m.
and p. m.
are nonstandard designators for AM and PM, you will need to create your own custom DateTimeFormatInfo
based on your desired locale (you are using the invariant locale currently) and modify its AMDesignator
and PMDesignator
properties. Then you can parse using the "tt"
format specifier as explained in this answer by Soner Gönül as well as this answer by Complexity, passing the custom DateTimeFormatInfo
as the IFormatProvider
to DateTime.ParseExact(string s, string format, IFormatProvider provider)
:
// Construct an invariant DateTimeFormatInfo and modify its AMDesignator and PMDesignator.
// The DateTimeFormat must be cloned since the global instance is read-only.
var formatInfo = (DateTimeFormatInfo)CultureInfo.InvariantCulture.DateTimeFormat.Clone();
formatInfo.AMDesignator = "a. m.";
formatInfo.PMDesignator = "p. m.";
var x = DateTime.ParseExact(date, "d/MM/yyyy h:mm:ss tt", formatInfo);
For performance reasons you may want to statically cache your DateTimeFormatInfo
as shown, e.g., here.
Alternatively, there are several cultures that do use a. m.
and p. m.
as AM and PM designators, mainly Spanish and Catalan, and your app may be running with one of these cultures as the current culture. You can discover all such cultures as follows:
foreach (var info in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
if (info.DateTimeFormat.AMDesignator == "a. m." && info.DateTimeFormat.PMDesignator == "p. m.")
{
Console.WriteLine("Culture {0} ({1}) has the required AM and PM designators.", info.DisplayName, info.Name);
}
}
Which outputs
Culture Catalan (ca) has the required AM and PM designators.
Culture Catalan (Andorra) (ca-AD) has the required AM and PM designators.
Culture Catalan (Catalan) (ca-ES) has the required AM and PM designators.
Culture Valencian (Spain) (ca-ES-valencia) has the required AM and PM designators.
Culture Catalan (France) (ca-FR) has the required AM and PM designators.
Culture Catalan (Italy) (ca-IT) has the required AM and PM designators.
Culture Spanish (Equatorial Guinea) (es-GQ) has the required AM and PM designators.
Culture Spanish (Mexico) (es-MX) has the required AM and PM designators.
Culture Spanish (Philippines) (es-PH) has the required AM and PM designators.
And you can examine the AM and PM designators of the current culture by printing to the console:
Console.WriteLine("Current culture AMDesignator=\"{0}\" and PMDesignator=\"{1}\".",
CultureInfo.CurrentCulture.DateTimeFormat.AMDesignator,
CultureInfo.CurrentCulture.DateTimeFormat.PMDesignator);
You could parse your date
string in any of these cultures and get your desired result:
var formatInfo = CultureInfo.GetCultureInfo("es-MX");
var x = DateTime.ParseExact(date, "d/MM/yyyy h:mm:ss tt", formatInfo);
And if your user's current culture is one of these cultures, you could pass CultureInfo.CurrentCulture
(or CultureInfo.CurrentUICulture
if appropriate):
var x = DateTime.ParseExact(date, "d/MM/yyyy h:mm:ss tt", CultureInfo.CurrentCulture);
Demo fiddle here.