3

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?

Community
  • 1
  • 1
devfunkd
  • 3,164
  • 12
  • 45
  • 73
  • Be sure to check that the time/date format settings on each operating system are matching. Users can specify if they want dashes or slashes--and coincidentally that's how you should probably format your date strings. I would hate to be forced to read date's in a specific format -_- – Kcvin Apr 22 '15 at 20:35
  • That's the work around right now for the users on Windows 8, they need to go into their DateTime settings and add slashes instead of dashes. But my application is suppose to use slashes, always. This is for display purposes in the UI it's whereever the code is doing DateTime.Now or DateTime.Parse, etc. – devfunkd Apr 22 '15 at 20:40
  • in one place the code is DateTime.Parse("2014/05/13") but the value after the parse is with dashes no matter what culture I provide and I don't know why. – devfunkd Apr 22 '15 at 20:45

2 Answers2

3

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)));
Jeff B
  • 8,572
  • 17
  • 61
  • 140
kmcnamee
  • 5,097
  • 2
  • 25
  • 36
  • They were added in .net 4.5 you will have to use Thread.CurrentThread and be carefully anytime you launch new threads that you set the culture on them... – kmcnamee Apr 22 '15 at 20:55
  • Thanks this solved it for me, I didn't realize I was on 4.0 so I upgraded at the same time too. Thank you! – devfunkd Apr 23 '15 at 03:05
-3

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);
TGarrett
  • 562
  • 4
  • 15
  • I need to set it globally, there are hundreds of appears in the code base that uses DateTime.Now or DateTime.Parse – devfunkd Apr 22 '15 at 20:50
  • Then make a date class that handles it, I just gave you a snippet of code to use, I am not writing your code for your work! You seem a little bit critical, just like the above response you were complaining about it wasnt working... – TGarrett Apr 22 '15 at 23:52
  • 1
    The question was about setting it globally, the answer didn't do that. I apologize for sounding that way, sorry. – devfunkd Apr 23 '15 at 03:03