Bug 58635 - Binding Conversion From DateTime Converts to Wrong Date Format
Summary: Binding Conversion From DateTime Converts to Wrong Date Format
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.5
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on: 60966
Blocks:
  Show dependency tree
 
Reported: 2017-08-08 00:28 UTC by Melbourne Developer
Modified: 2018-03-14 18:17 UTC (History)
7 users (show)

Tags: ac ios android uwp bindings dateformat datetime culture
Is this bug a regression?: ---
Last known good build:


Attachments
repro project (296.52 KB, application/x-zip-compressed)
2017-08-09 20:58 UTC, Jimmy [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Melbourne Developer 2017-08-08 00:28:09 UTC
I work in en-AU format. My devices (Android, and Windows UWP) both default to en-AU culture. When I call ToString() on a DateTime, it correctly converts the DateTime to an en-AU formatted date. However, if I bind a Text property (Label, Editor, Entry) to a DateTime property, the string is formatted, and displayed in en-US format. This is easy to recreate.

To recreate:

-Switch the culture of your device and Date format to en-AU (day/month/year)
-Clone this repo: https://ChristianFindlay@bitbucket.org/ChristianFindlay/xamarin-forms-scratch.git
-Run the UWP, or Android sample, and click "Date Display"
-Notice that the first date is displayed as "31/1/2000 12:00:00 AM" (This is manually converted from DateTime to string with ToString().
-Notice that all other displayed dates which are converted using XF's core conversion code are displayed in en-US format as "01/31/2000 00:00:00"
-I.e. ToString() is giving a different value to the displayed binding. The bug is that binding is not calling ToString() to convert from DateTime to string. It is doing something different and therefore not correctly formatting the date

This is the code

    public partial class DateDisplayPage : ContentPage
    {
        public DateDisplayPage()
        {
            InitializeComponent();

            var date = new DateTime(2000, 1, 31);
            ToStringBox.Text = date.ToString();
            BindingContext = new TestModel { TheDate = date };
        }
    }

    public class TestModel
    {
        public DateTime TheDate { get; set; }
    }

This is the XAML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestXamarinForms.DateDisplayPage">
    <ContentPage.Content>

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="50*" />
                <RowDefinition Height="50*" />
                <RowDefinition Height="50*" />
                <RowDefinition Height="50*" />
            </Grid.RowDefinitions>

            <Label x:Name="ToStringBox"  VerticalOptions="Center" HorizontalOptions="Center" />
            <Label Text="{Binding TheDate}" Grid.Row="1" VerticalOptions="Center" HorizontalOptions="Center" />
            <Editor Text="{Binding TheDate}" Grid.Row="2"  VerticalOptions="Center" HorizontalOptions="Center" />
            <Entry Text="{Binding TheDate}" Grid.Row="3" VerticalOptions="Center" HorizontalOptions="Center" />

        </Grid>

    </ContentPage.Content> 
</ContentPage>

Here is a screenshot. You can see that the date converted using ToString() is correct, while the others are incorrectly formatted:
https://www.dropbox.com/s/zm1zcea1diej5ip/dates.png?dl=0
Comment 1 Jimmy [MSFT] 2017-08-09 20:58:50 UTC
Created attachment 24111 [details]
repro project

I ran the code you provided and I am seeing the issue. I am confirming the report and we will investigate further.


### Steps to Reproduce
1. On your test device, change the region/culture to one that does not use the month-date-year date format, e.g Australia, Germany
2. Run the attached project


### Expected Results
All the controls will display the date in the same format.


### Actual Results
Only the first Label displays the date in the correct format for the device. The other controls use bindings to display the date and still use the en-US format.


### Version Tests
2.3.6.126   BAD
2.3.5-pre6  BAD
2.3.4.247   BAD
Comment 2 Melbourne Developer 2017-08-09 22:49:33 UTC
You have made me very happy.
Comment 3 Melbourne Developer 2017-08-30 01:45:22 UTC
Hi. Is there any update on this, or workarounds? This will essentially mean that before we can release our app we will have to manually put date formatting with converters in to every piece of XAML in the system which we will later have to remove because hard coding the date format breaks the rules for internationalization. This is very large problem.
Comment 4 Melbourne Developer 2017-09-07 05:31:49 UTC
Hello...
Comment 5 Melbourne Developer 2017-09-15 04:25:09 UTC
Hi. Any plans to fix this bug?

It affects every user of Xamarin technology outside the USA. Most people in the world don't read dates the same way as people in the USA do. When dates get displayed in USA format, bug reports land in our Inbox.
Comment 6 Melbourne Developer 2017-09-25 04:27:20 UTC
This is a very bad bug. Is anyone looking in to this?
Comment 7 Melbourne Developer 2017-10-10 04:12:14 UTC
Hi. What's happening with this bug?
Comment 8 Sigvald 2017-10-17 07:45:20 UTC
Hi,

I'd just like to add that for all of us developing app's outside of the US, this is an actual problem.
Is anything being done to fix this issue, is it in the pipeline?
Comment 9 Melbourne Developer 2017-10-17 22:16:04 UTC
Agreed.

I often have to reiterate this point, but there are cultures outside of the USA, and we don't have the option of displaying dates to our users in USA format.
Comment 10 Melbourne Developer 2018-02-08 05:05:30 UTC
I can't believe that this bug is still here!!!!

This bug was confirmed by Xamarin months ago. Do you literally not care that dates are hard coded formatted in US format? 

Seriously, where is the sense of prioritization?

Xamarin Forms is implementing CSS? At a time when we can't even get dates displayed in our locale's native format?

To make matter 10x worse, the hack converter that I put in place to work around this issue now goes in an infinite loop. If I use this converter anywhere, it slows the app to a grinding halt. The binding just keeps hitting the Convert method in an infinite loop.

    public class DisplayDateInLocalFormatConverter : IValueConverter
    {
        #region Fields
        private const string NoDateSelected = "No Date Selected";
        #endregion

        #region Public Methods
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var emptyDateDisplay = (string)parameter ?? NoDateSelected;

            if (value is DateTime theDate)
            {
                return theDate == DateTime.MinValue ? emptyDateDisplay : theDate.ToString("dd/MM/yyyy HH:mm");
            }

            return emptyDateDisplay;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
        #endregion
    }
Comment 11 Melbourne Developer 2018-02-08 05:08:28 UTC
Are we just going to have to tell our customers to learn to read dates backwards?
Comment 12 Jason Smith [MSFT] 2018-02-15 20:07:39 UTC
This is working as intended, the data binding uses the invariant culture and is consistent with the way that WPF works - we are going to add capabilities to make this simpler to allow the binder to use a user-defined culture, in the meantim, you can use the options outlined in this doc https://paper.dropbox.com/doc/Answers-for-DateTime-conversion-la24VtVd0m2L6nSijlVht?_tk=share_copylink
Comment 13 Melbourne Developer 2018-03-08 03:53:27 UTC
Incorrect. 

WPF has a different formatting bug that causes the same result for a different reason. In WPF there is a property called "Language" on FrameworkElement. This defaults to en-US even when your regional settings are set to en-AU. This is a bug in WPF. The fix for this bug is to add this line of code in WPF:

Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);

You can see the working sample here. Notice that if you comment out the above line, you will see the same incorrect behavior as Xamarin Forms. Xamarin Forms Pages and Views do not have a "Language" property so it is not possible to fix the problem in the same way in Xamarin Forms. It means that every single displayed DateTime needs to have the StringFormat set.

https://www.dropbox.com/s/2qcacomtsjweb1o/DateTimeWPF.7z?dl=0

This is clearly a glaring bug, and all Xamarin Forms users outside the USA will be frustrated by it. Shall I move this bug to Github?
Comment 14 Melbourne Developer 2018-03-14 02:00:06 UTC
Bug moved to Gituhb:
https://github.com/xamarin/Xamarin.Forms/issues/2049
Comment 15 Samantha Houts [MSFT] 2018-03-14 18:17:37 UTC
Please follow the issue on GitHub for updates. Thank you.