Entering date in d/mm format in TextBox
Asked Answered
A

2

10

I've got a TextBox bound to a nullable DateTime property.

I'm in Australia, so I want dates presented in the d/mm/yyyy format.

On my Windows 7 box, I can enter the date in d/mm format, and it's converted correctly (eg. 1/11 converts to November 1st, 13/1 converts to January 13th etc.)

On my Windows 8 box, the same input is interpreted as if it was in the US format, so 1/11 converts to January 11th, and 13/1 fails (since there is no 13th month).

Both computers are set to use the Australian formats, and I have this code in the Application.StartUp event:

FrameworkElement.LanguageProperty.OverrideMetadata(GetType(FrameworkElement), New FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)))

The Binding's StringFormat is set to d/MM/yyyy, and I've checked that this is correct by also binding a TextBlock to the same property that has its StringFormat set to D (the long date format, which gives values like 'Wednesday, 11 January, 2012').

Does anyone have any ideas?

Update: Further investigation (see comments below) reveals this seems to be an issue with the en-AU culture being different on Windows 8 compared to Windows 7, which means that it interprets dates like '1/11' in mm/dd format on Windows 8, whereas on Windows 7, it interprets them in dd/mm format, which is what I'd expect when using the en-AU culture.

Azo answered 4/11, 2012 at 0:2 Comment(8)
Other things I've tried: setting xml:lang="en-AU" on the parent Window, setting ConverterCulture=en-AU on the Binding. Neither worked... (there isn't a converter on the Binding, but I figured I'd try it anyway)Azo
DateTime.Parse("1/11", CultureInfo.GetCultureInfo("en-AU")).Month gives 11 on Windows 7, but 1 on Windows 8 - this isn't a WPF bug any more...Azo
On Windows 8: en-AU gives 1, en-GB gives 11, en-US gives 1, fr gives 11Azo
How about a workaround where you use a ValueConverter instead. Is this a OneWay or TwoWay binding?Butterbur
It's TwoWay. (I ended up just changing the LanguageProperty override to use en-GB for the time being, which isn't perfect, but it's pretty good.)Azo
Given what you've found, you may want to adjust the question, and especially the tags. Sounds like us Aussies have a bad registry entry in Win8.Glenoid
@MarkHurd I'm not sure whether it is a registry setting or not but we have recently had a similar issue on windows server 2012. The above line returns the month as 1. So its not confined to Win8.Rohde
@JamesKhoury It hasn't affected me yet, but it looks like it should be listed on Connect as a problem then. (May be it is...)Glenoid
N
1

Since IetfLanguageTag is deprecated, have you considered using the Name property instead?

http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.ietflanguagetag.aspx:

The format of an IETF language tag is similar to the culture name returned by the Name property, but does not identify a culture uniquely. That is, different cultures share the same IETF language tag if those cultures have identical linguistic characteristics.

Narine answered 6/1, 2013 at 6:36 Comment(1)
Using Name instead of IetfLanguageTag makes no difference, they're the same, both on Windows 7 and Windows 8. (As that page says - "IETF tags and names are identical.")Azo
G
0

Using

System.Globalization.DateTimeFormatInfo.CurrentInfo.MonthDayPattern = "dd MMMM";

Will update your current thread's Month and Day format. The MonthDayPattern changed in Windows Server 2012. I haven't figured out why yet.

Grannie answered 7/7, 2016 at 0:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.