Search code examples
c#globalizationdatetime-format

Why am I getting incorrect time format from CultureInfo?


I have to display a current time on my application during certain operations, if current system time format is HH:mm:ss then if user change it to hh:mm:ss tt when app is still running it does not pick up the changes! It still displays time in HH:mm:ss.

I tried using DateTime.Now.ToLongTimeString() and DateTime.Now.ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern);

Is there anything missing? Any suggestions to fix the problem?


Solution

  • I strongly suspect that the framework is simply caching the pattern used for the time format, and won't normally pick it up again until you restart the app.

    You could try calling ClearCachedData and see if it then goes back to the OS for the system time format. (I don't know whether there's anything you can monitor to work out when the format has changed - you don't want to be clearing the cache all the time, of course.)