WPF Default DateTime format

匆匆过客 提交于 2019-12-04 14:17:18
kmcnamee

Here is what I use in OnStartup. It's different than the SO question you linked to as I use DefaultThreadCurrentCulture as it sets the default for the entire process and any threads launched.

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)));

This will provide you with what you are looking for, or you can use 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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!