문제

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"; 
도움이 되었습니까?

해결책

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)));

다른 팁

Apparently, the locale settings for "en-IE" uses a 24-hour clock by default, so there is no need for AM/PM.

This is a behavior of Culture. It uses windows cultural settings to manipulate its details. if you set locale information as you wish , you should set what are the AM/PM Designators. That is what you have done by using @Jordans code.

But the problem here is language. 'AM' and 'PM' comes to any locale. but what need to be happened is 'AM' and 'PM' should change according to the locale.

E.G : if your locale information is "zh-TW" result would be 下午 04:16:08.

So Using 24 hour clock is a one solution. but no AM/PM as we know E.G 3.15 PM = 15:15 this is a one of the good solution because this is a stranded.

Second solution which is offer is this. changing the locale/cultural setting of the server. please refer https://msdn.microsoft.com/en-us/library/bb688125.aspx

Hope my comment would help you.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top