Search code examples
c#datetimewindows-store-apps

Respect users date time format settings in Windows Store app


In a Windows Store app (8.1, non-UWP), how do I get the date and time formats as configured in Windows 10? In the following example, the operating system is installed with language en-US and time and currency formats set to de-DE.

Settings in Windows 10

I tried:

new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("shortdate"); // resolved language = en-US, resolved geographic region = ZZ ?!

Windows.Globalization.Language.CurrentInputMethodLanguageTag; // en-US

Windows.System.UserProfile.GlobalizationPreferences.Languages; // en-US
Windows.Globalization.ApplicationLanguages.Languages; // en-US

DateTime.Now.ToString(); // en-US

CultureInfo.CurrentCulture; // en-US
CultureInfo.CurrentUICulture; // en-US

new GeographicRegion().CodeTwoLetter; // DE, but: could be ch-DE, de-DE, xyz-DE ?!

I read this post on SO, but I fail to see how the highest voted answer will allow me to respect the date and time format set by the user (without doing wild guesses based on language preferences).

So, is there a way to format a DateTime instance as expected in a Windows Store app?


Solution

  • You can get the correct culture information (the one the user has set in the Regional Settings) through the GetLocaleInfoEx Win32 API

    Please check this post for more information on this matter.