Wrong date format in xamarin forms, how to solve?

1

In my mobile application I have a part that I retrieve the local date and time, so the date is bringing the month first, then the day and last year, how to change it, here is my code:

In my layout file:

<Label Text="Data" FontAttributes="Bold"/>
<Label Text="{Binding DataAtual}"/> 

Here is the class created for the logical part:

public class DataHora : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private DateTime _dataAtual;
    public DateTime DataAtual
    {
        set
        {
            if (_dataAtual == value)
                return;

            _dataAtual = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DataAtual)));
        }
        get
        {
            return _dataAtual;
        }
    }

    public DataHora()
    {
        DataAtual = DateTime.Now;

        Device.StartTimer(TimeSpan.FromSeconds(1), () =>
        {
            DataAtual = DateTime.Now;
            return true;
        });
    }
}

And here in my mainpage file:

BindingContext = new DataHora();
    
asked by anonymous 15.02.2018 / 18:00

1 answer

1

Binding supports defining the string format you want to display.

Simply change the Binding of the label in your XAML to the following:

<Label Text="{Binding DataAtual, StringFormat='{0:dd/MM/yyyy HH:mm:ss}'}"/>

If you only do Binding raw, the default behavior is ToString() in the object, and in that case, it is certainly following the date format defined in your emulator culture, most likely en-US ; >     

15.02.2018 / 20:09