Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format date shown on DatePicker

I created a Pivot Project on Visual Studio 2013.

I created the following control:

<DatePicker Header="Data" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Width="341"/>

When I tap on it on my phone, it displays the date as M/D/Y. Is it possible to display it as D/M/Y?

Also, don't know if I should create another question, but how can I translate to pt-BR the day/month names shown within the control? As well as that "Choose a date".

enter image description here

like image 547
undefined Avatar asked Nov 01 '22 03:11

undefined


1 Answers

The DatePicker format is using user's preferred language and region. This control will automatically look different, for my region it's day/month/year like you wish it to be. I've tried to force the control to use other language, but it seems to be taking it directly from phone settings. Some information you may find here at MSDN:

If you need to allow users to choose a date or select a time, use the standard date and time picker controls. These will automatically use the date and time formats for the user's preferred language and region.

The other bad news you can find here at MSDN about formating that particular control:

Note This section applies to Windows Store apps using C++, C#, or Visual Basic. Formatting is ignored in Windows Phone Store apps.

So in current API it may be hard to change the formatting.

The good news is for the CHOOSE DATE text at the top - it's automatically localized, depending on user's language and region. So you don't have to worry about it if your app supports user's language. But I've not found way to change it to other text.

As for the text displayed inside the button before it's clicked, you can always use Button with DatePickerFlyout, a simple example with apropriate converter:

<Button Content="{Binding ElementName=chosenDate, Path=Date, Converter={StaticResource DateFormatConverter}}">
    <Button.Flyout>
        <DatePickerFlyout x:Name="chosenDate" />
    </Button.Flyout>
</Button>

and the converter class:

public class DateFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        DateTimeOffset chosen = (DateTimeOffset)value;
        return string.Format("{0}/{1}/{2}", chosen.Day, chosen.Month, chosen.Year);
        // or use chosen.ToString() with format provider
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); }
}
like image 72
Romasz Avatar answered Nov 09 '22 08:11

Romasz