Recognition of changed culture or time format

Heiko 1,286 Reputation points
2022-06-16T11:58:55.453+00:00

I want to show the current day time in the current time format like:

DateTime now = DateTime.Now;  
textBlockTime.Text = now.ToString("T", culture);  

On app start the culture is set to CultureInfo.CurrentCulture.

When the user changed the culture or time format in Windows settings, I noticed that the Window receives the WM_WININICHANGE (WM_SETTINGCHANGE) message. So I could react and show the newly set time format, but how can I get to the new used culture or time format? In Windows setting it is also possible to change the time format without changing the culture.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,815 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,218 questions
{count} votes

Accepted answer
  1. Castorix31 86,496 Reputation points
    2022-06-16T14:35:40.083+00:00

    You can use GetLocaleInfoEx in WM_SETTINGCHANGE to get the current formats

    For example, a test for the Short Time :

                var sbDataUser = new StringBuilder(128);  
                if (GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SSHORTTIME, sbDataUser, sbDataUser.Capacity) != 0)  
                {  
                    var sbDataSystem = new StringBuilder(128);  
                    if (GetLocaleInfoEx(LOCALE_NAME_SYSTEM_DEFAULT, LOCALE_SSHORTTIME, sbDataSystem, sbDataSystem.Capacity) != 0)  
                    {  
                        MessageBox.Show(string.Format("Time Format (User) : {0}\r\nTime Format (System) : {1}", sbDataUser.ToString(),  sbDataSystem.ToString()), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);  
                    }  
                }  
    

    Declarations (cannot post here) : 107437


1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,471 Reputation points
    2022-06-16T12:41:40.527+00:00

    See the following

    https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

    DateTimeFormatInfo.CurrentInfo.LongTimePattern = "TODO";  
    DateTimeFormatInfo.CurrentInfo.ShortTimePattern = "TODO";  
    

    Or

    DateTimeFormatInfo formatInfo = new DateTimeFormatInfo()  
    {  
        LongTimePattern = "TODO",  
        ShortTimePattern = "TODO"  
    };  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.