none
How do I change SelectedDateFormat to display 21 Nov 2019? RRS feed

  • Question

  •  <DatePicker CalendarStyle=... SelectedDateFormat="Short" ...> displays 11/21/2019.  How do I change SelectedDateFormat to display 21 Nov 2019?

    bhs67

    How do I change SelectedDateFormat to display 21 Nov 2019?

        <DatePicker CalendarStyle="{StaticResource resizedCalendarItem}"
            SelectedDateFormat="Short"
            x:Name="gDPickVisitDate"
            SelectedDateChanged="gDPickVisitDate_SelectedDateChanged" />

    displays 11/21/2019.

    Note that Changing the string format of the WPF DatePicker does display "21 Nov 2019" in the dialog box.

    However, string stDate = gDPickVisitDate.ToString results in stDate containing "11/21/2019 12:00:00 AM".


    • Edited by bhs67 Thursday, November 21, 2019 7:35 PM
    Thursday, November 21, 2019 4:57 PM

Answers

  • Hi,
    use the overloaded method ToString:

    string stDate = string.Empty; if (gDPickVisitDate.SelectedDate.HasValue) stDate = gDPickVisitDate.SelectedDate.Value.ToString("dd MMM yyyy");



    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    • Edited by Peter Fleischer Friday, November 22, 2019 4:27 PM correct code
    • Marked as answer by bhs67 Friday, November 22, 2019 4:44 PM
    Friday, November 22, 2019 1:20 PM

All replies

  • Hi,
    the only simple way is

    SelectedDateFormat="Long" 

    this format will show: Thursday, 21 November 2019.


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Thursday, November 21, 2019 9:17 PM
  • Hi,

    You can use style to set the datetime display format of DatePicker

      <Window.Resources >
            <Style TargetType="DatePickerTextBox">
                <Setter Property="Control.Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <TextBox Text="{Binding Path=SelectedDate, StringFormat='dd MMM yyyy', RelativeSource={RelativeSource AncestorType=DatePicker}}" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Window.Resources>
        <Grid>
            <DatePicker  
       
            x:Name="gDPickVisitDate">
               
            </DatePicker>
        </Grid>

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 22, 2019 7:11 AM
    Moderator
  • Alex, Implementing your code does display "21 Nov 2019" in the dialog box, just as Changing the string format of the WPF DatePicker does.

    However, string stDate = gDPickVisitDate.ToString results in stDate containing "11/21/2019 12:00:00 AM".



    bhs67


    • Edited by bhs67 Friday, November 22, 2019 12:55 PM
    Friday, November 22, 2019 12:55 PM
  • Hi,
    use the overloaded method ToString:

    string stDate = string.Empty; if (gDPickVisitDate.SelectedDate.HasValue) stDate = gDPickVisitDate.SelectedDate.Value.ToString("dd MMM yyyy");



    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    • Edited by Peter Fleischer Friday, November 22, 2019 4:27 PM correct code
    • Marked as answer by bhs67 Friday, November 22, 2019 4:44 PM
    Friday, November 22, 2019 1:20 PM
  • Using string st = gDPickVisitDate.ToString("dd MMM yyyy") => the compiler complains "No Overload for method 'ToString' ..."

    bhs67

    Friday, November 22, 2019 4:01 PM
  • Hi,
    excuse me, I had corrected the code. You must use the Value of the SelectedDate property.

    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Friday, November 22, 2019 4:29 PM
  • Vielen Dank!

    bhs67

    Friday, November 22, 2019 4:44 PM