CurrentCulture.DateTimeFormat.LongTimePattern Read Only

I am trying to set the LongTimePattern CurrentCulture property with the following code:

System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongTimePattern = "HH:mm:ss"; 

and I get InvalidOperationException: Instance is read-only.

Any idea how I can change it? I want to make LongTimePattern show a 24-hour format for any culture.

0
c # datetime time format readonly
source share
3 answers

If you modify the System.Threading.Thread.CurrentThread.CurrentCulture file, it will automatically update LongTimePattern.

You cannot update the currently assigned culture information, but create new and assign it to the current culture.

 System.Globalization.CultureInfo c = new System.Globalization.CultureInfo("es-ES"); c.DateTimeFormat.LongTimePattern = "h-mm-ss"; Thread.CurrentThread.CurrentCulture = c; 
+3
source share

If you just want to change the ONE or TWO values ​​and leave everything else the same, you can use Clone to get the current culture record, for example:

 CultureInfo i; i = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone(); i.DateTimeFormat.LongTimePattern = "HH:mm:ss"; i.DateTimeFormat.ShortTimePattern = "HH:mm"; Thread.CurrentThread.CurrentCulture = i; i = (CultureInfo)Thread.CurrentThread.CurrentUICulture.Clone(); i.DateTimeFormat.LongTimePattern = "HH:mm:ss"; i.DateTimeFormat.ShortTimePattern = "HH:mm"; Thread.CurrentThread.CurrentUICulture = i; 

This seems better than using a culture string to get an initial culture.

+1
source share

I'm not sure that you can change cultures by letting you do this in order to defeat the goal of having cultures in the first place - they should display the date and time in the generally accepted format used by this culture.

If you want to show a different format, you can always use your own date / time format.

See http://msdn.microsoft.com/en-us/library/az4se3k1.aspx for all available pre-installed formats and detailed information on how to display your own format.

0
source share

All Articles