Rob.Oakshott Rob.Oakshott - 4 months ago 16
ASP.NET (C#) Question

Why is en-IE culture not giving me AM PM in format string?

I have this code to set the culture

public static CultureInfo GetRegionDefaultCulture(int regionId)
{
// removed code to shorten....
return CultureInfo.CreateSpecificCulture("en-au");

}


using it here in the code behind in my test example

Thread.CurrentThread.CurrentCulture =
CultureHelper.GetRegionDefaultCulture(regionId);


using this in aspx code behind of my test example

DateTime dtNow = DateTime.Now;
Response.Write("<br/>" + dtNow.ToString("dd MMM yyyy hh:mm tt"));


outputs

09 Nov 2010 04:42 PM


If I update the code to use any of these cultures, I also correctly get AM/PM in the output

en-PH
en-GB
en-US
en-JM


If I update the code to use

en-IE


the output is

09 Nov 2010 04:44


Am I missing something? Why is en-IE behaving differently?

Answer
Gained from @Jordans answer.

Adding this bit of code worked.

DateTimeFormatInfo info = CultureInfo.CurrentCulture.DateTimeFormat;
info.AMDesignator = "AM";
info.PMDesignator = "PM";

Answer

According to the documentation, if the culture doesn't use AM/PM, then nothing will be returned by tt.

For cultures that do not use an AM designator, this property returns an empty string.

I can't find any documentation on which cultures have an AM designator. The culture info is retrieved via a native method for which I can't find the source. You can generate your own list with the following code:

CultureInfo.GetCultures(CultureTypes.AllCultures)
  .Where(c => string.IsNullOrEmpty(c.DateTimeFormat.AMDesignator)));