Working on a large WPF application and having some issues with the DateTime format, it's a huge codebase and many places do not specify the culture.
When the user opens the application on Windows 7 or 8 the datetime format is different (Win 7 uses slashes and Win 8 uses dashes).
I tried setting the Culture to "en-US" in the Application Startup (see below link) but it doesn't seem work?
Setting Culture (en-IN) Globally in WPF App
How do I set my WPF application to not use the culture of the user machine?
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);