-1

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".

Compiler does not like gDPickVisitDate.Value. I tried GetValue with no success.

ttom
  • 985
  • 3
  • 12
  • 21
  • 2
    Possible duplicate of [Changing the string format of the WPF DatePicker](https://stackoverflow.com/questions/3819832/changing-the-string-format-of-the-wpf-datepicker) – Longoon12000 Nov 21 '19 at 17:04
  • What do you mean by _However, gDPickVisitDate still contains 11/21/2019._? If changing the format displays correctly, what else are you looking for? – Sach Nov 21 '19 at 19:06
  • [From the docs](https://learn.microsoft.com/en-us/dotnet/api/system.datetime?view=netframework-4.8) "The default DateTime.ToString() method returns the string representation of a date and time value using the current culture's short date and long time pattern." – jnthnjns Nov 21 '19 at 20:07
  • 1
    If you want the `ToString` of a DateTime object to be in your format, you must specify it `gDPickVisitDate.Value.ToString("dd MMM yyyy")` – jnthnjns Nov 21 '19 at 20:08

2 Answers2

0

The simplest soulution would be:

gDPickVisitDate.ToString("dd MMM yyy"));
//dd/MMM/yyyy: 21 Nov 2019

Or as described by Microsoft here

 //Create a new DateTimeFormatInfo where you also can set your CultureInfo
 DateTimeFormatInfo dtfi = CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat;
 //Choose the separator
 dtfi.DateSeparator = " ";
 //Choose the format
 dtfi.ShortDatePattern = @"dd/MMM/yyyy";
 //Write it to the console
 Console.WriteLine("   {0}: {1}", dtfi.ShortDatePattern, 
                                   gDPickVisitDate.ToString("d", dtfi));

// The example displays the following output:
//       Original Short Date Pattern:
//          dd/MMM/yyyy: 21 Nov 2019

If you wonder wich formats or outputs are possible, check this site by Microsoft.

Julian
  • 886
  • 10
  • 21
0

Thanks to Peter Fleischer => https://social.msdn.microsoft.com/Forums/vstudio/en-US/3ac5b308-5bd7-4a6b-bf92-1ab4ebe258b1/how-do-i-change-selecteddateformat-to-display-21-nov-2019?forum=wpf

string stDate = string.Empty;
if (gDPickVisitDate.SelectedDate.HasValue)
  stDate = gDPickVisitDate.SelectedDate.Value.ToString("dd MMM yyyy");
ttom
  • 985
  • 3
  • 12
  • 21