WPF toolkit, change date format in DatePicker

Kamilos picture Kamilos · Oct 8, 2009 · Viewed 7.3k times · Source

default date format in DatePicker is "yyyy-mm-dd" but i need "dd-mm-yyyy". This one solution doesn't work for me.

Answer

Kirkaiya picture Kirkaiya · Feb 2, 2011

This is a pretty old question (marked somewhat incorrectly as "answered"). If you need to use a specific date format for the WPF DatePicker control but don't want to open up the source-code for it (which is, in fact, available for download), you can always specify the DateFormat of your entire UI, or set it for each DatePicker control.

In my app, I used the following in Application_Startup event:

Private Sub Application_Startup(ByVal sender As Object, ByVal e As System.Windows.StartupEventArgs) Handles MyBase.Startup

   ' other application initialization code here

   Dim culInfo As Globalization.CultureInfo = New System.Globalization.CultureInfo("en-us")
   Dim dtinfo As Globalization.DateTimeFormatInfo = New System.Globalization.DateTimeFormatInfo() With {
       .ShortDatePattern = "dd-MMM-yyyy",
       .ShortTimePattern = "hh:mm:ss tt",
       .TimeSeparator = ":",
       .MonthNames = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.MonthNames(),
       .AbbreviatedMonthNames = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.AbbreviatedMonthNames(),
       .DateSeparator = "-"}

 culInfo.DateTimeFormat = dtinfo
 System.Threading.Thread.CurrentThread.CurrentCulture = culInfo

Note that this will set the format for short-date-format everywhere in your app (which is what I wanted, rather than going through and applying the culture to each and every instance of a date-picker. I came up with the above based on another StackOverflow post; I don't remember exactly where it was (or I'd credit the author), but anyway, hope this helps!

Kirk