WPF default date format

Working on a large WPF application and having some problems with the DateTime format, this is a huge code base and many places don't define culture.

When the user opens the app in Windows 7 or 8, the date and time format is different (Win 7 uses slashes and Win 8 uses dashes).

I tried setting Culture to "en-US" in app launch (see link below) but it doesn't seem to work? Setting Culture (en-IN) Globally in WPF Application

How do I configure my WPF application not to use the culture of the user machine?

+3


source to share


2 answers


This is what I am using in OnStartup

. It differs from the SO question you linked to as I use DefaultThreadCurrentCulture

, as it sets a default for the whole process and all running threads.

DefaultThreadCurrentCulture

and DefaultThreadCurrentUICulture

are in .NET 4.5 and later.



var newCulture = new CultureInfo(displayCulture);

// Set desired date format here
newCulture.DateTimeFormat.ShortDatePattern = "dd MMM yyyy";

// You cannot use DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture if 
// you dont have .NET 4.5 or later. Just remove these two lines and pass the culture 
// to any new threads you create and set Thread.CurrentThread...
CultureInfo.DefaultThreadCurrentCulture = newCulture;
CultureInfo.DefaultThreadCurrentUICulture = newCulture;

Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = newCulture;

FrameworkElement.LanguageProperty.OverrideMetadata(
    typeof(FrameworkElement),
    new FrameworkPropertyMetadata(
        System.Windows.Markup.XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

      

+3


source


This will provide you with what you are looking for, or you can use a regex ...



    DateTime dateTime;

    bool validDate = DateTime.TryParseExact(
        "04/22/2015",
        "MM/dd/yyyy", // or you can use MM.dd.yyyy
        CultureInfo.InvariantCulture,
        DateTimeStyles.None,
        out dateTime);

      

-1


source







All Articles