I have a WPF C# application (.NET 4.0) and I have a date picker. I would like to know if I can change the format date (the text property of the date picker) to the yyyy/MM/dd
format.
I am using the MVVM pattern, so I would like to know the XAML code, if it is possible to do it in this place.
To set and get the input type date in dd-mm-yyyy format we will use <input> type attribute. The <input> type attribute is used to define a date picker or control field. In this attribute, you can set the range from which day-month-year to which day-month-year date can be selected from.
Do one of the following: For a text box control or a date picker control, ensure that the Data type list displays the appropriate data type, and then click Format. For an expression box control, ensure that the Format as list displays the appropriate data type, and then click Format.
If you only want to insert the date, use the Date() function in a desktop database, or the Today() function in an Access web app.
DateTimePicker allows you to define the text representation of a date and time value to be displayed in the DateTimePicker control. The format specified is achieved by the dateTimeFormat property. Default value of this property is M/d/yyyy h: mm tt.
I can use this code:
<DatePicker SelectedDate="{Binding myVideModelProperty}"
Height="25" HorizontalAlignment="Left" Margin="81,-2,0,0" Name="myDatePicker" VerticalAlignment="Top" Width="115">
<DatePicker.Resources>
<Style TargetType="{x:Type DatePickerTextBox}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<TextBox x:Name="PART_TextBox"
Text="{Binding Path=SelectedDate, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, StringFormat={}{0:yyyy/MM/dd}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DatePicker.Resources>
</DatePicker>
Using the datePicker resources I can set the format of its textBox. In this way, It is not needed a converter, neither a general template and any other extra code.
According to this post you can change the culture to get the correct date format
System.Globalization.CultureInfo culInfo = new System.Globalization.CultureInfo("en-us");
System.Globalization.DateTimeFormatInfo dtinfo = new System.Globalization.DateTimeFormatInfo();
dtinfo.ShortDatePattern = "yyyy/MM/dd";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With