WPF DatePicker dateformat change issue

Bikramj 21 Reputation points
2021-06-29T07:28:06.617+00:00

Hi ,

i have used a datepicker inside my code .
I want the format as dd/MM/yyyy but i am getting it always as MM/dd/yyyy.
Here is the snippet for the datepicker code.
Can anyone help please.
<DatePicker Name="dateTimePicker1" Grid.Row="2" Grid.Column="3" Margin="0,0,57,0" ToolTip="{Binding Mode=TwoWay,Path=.}"
SelectedDate="{x:Static s:DateTime.Now}" SelectedDateChanged="dateTimePicker1_SelectedDateChanged"
materialDesign:HintAssist.Hint="Pick Date"
Foreground="Black" VerticalAlignment="Bottom" >
<DatePicker.Resources>
<Style TargetType="DatePickerTextBox" >
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>

                                                    <TextBox x:Name="DP_TextBox" Text="{Binding Path=SelectedDate,StringFormat={}{0:dd/MM/yyyy},RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}"/>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>

                                    </Style>
                                </DatePicker.Resources>





                            </DatePicker>

Thanks

Developer technologies Windows Presentation Foundation
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,646 Reputation points
    2021-06-30T03:19:05.37+00:00

    You may be not able to set the format of DatePicker SelectedDate to dd/MM/yyyy in xaml , I tested as below and it showed an error.
    110408-capture.png

    There is a description A date that is in one of the formats that are listed in the DateTime XAML Syntax topic. in the document DatePicker.SelectedDate Property XAML Values part. (DateTime XAML Syntax)

    You can use below method to update format in C# code:

    Method 1:

    var txt = this.dateTimePicker1.SelectedDate.Value.Date.ToString("dd/MM/yyyy")  
    

    Method 2:

    CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);  
                ci.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";  
                Thread.CurrentThread.CurrentCulture = ci;  
      
                var txt = this.dateTimePicker1.SelectedDate.Value.Date.ToShortDateString();  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.