Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting date from DatePicker in format yyyy/MM/dd

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.

like image 227
Álvaro García Avatar asked Dec 22 '12 11:12

Álvaro García


People also ask

How do I change the DatePicker format to yyyy mm dd in HTML?

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.

How do I change date format in date picker?

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.

How can I get current date in DatePicker?

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.

What is DatePicker format?

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.


2 Answers

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.

like image 198
Álvaro García Avatar answered Nov 07 '22 13:11

Álvaro García


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";
like image 31
Mattias Josefsson Avatar answered Nov 07 '22 11:11

Mattias Josefsson