question

Bikramj-4545 avatar image
0 Votes"
Bikramj-4545 asked DaisyTian-1203 answered

WPF DatePicker dateformat change issue

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

windows-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


It seems to work. Where are you getting it always as MM/dd/yyyy?

0 Votes 0 ·

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered

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.


capture.png (10.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.