I'm based in the UK, and my computer always shows a UK date format (dd/mm/yyyy).
My WPF project is, for some reason converting my DateTime format to US type. This occurs on my work machine and home machine.
I've been able to replicate the issue very simply
<Window x:Class="TimeIssues.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Text="{Binding MyTime}"></TextBlock> <!-- the value is 1/28/2014 10:15:37 (note it is M/dd/yyyy....)
</Grid>
</Window>
And in the code behind, simply
public partial class MainWindow : Window
{
public MainWindow()
{
SetMyTime("28/01/2014 10:15:37"); //note, this is dd/MM/yyyy .... format
this.DataContext = this;
InitializeComponent();
}
private void SetMyTime(string dateTime)
{
DateTime dt;
DateTime.TryParseExact(dateTime, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
MyTime = dt; // the value is expected, showing 28/01/2014 10:15:37
}
public DateTime MyTime { get; set; }
}
Just to confirm that this isn't an IT issue (or so I think), my PC is set for UK (and the Location is also set to UK as is the Current language for non-unicode programs)
I have read WPF format DateTime in TextBlock? but this won't help (I don't think) as that question is about setting the format when the format is known.
If I update my MyTime from DateTime
to a string
, then the result is expected (UK format)! The issue is WPF changes the format when the type is DateTime which is not desired!
My issue is 2 fold -
- I won't know the format the user needs (my software is distributed world wide).
- WPF appears to change the format incorrectly any way!
How do I tell WPF to use the user's locale settings? Is there any way to read the OS settings (as per the screen shot above)?