Applying Custom Date Format on Silverlight DatePicker

Problem:

I want to display date in DatePicker Control like yyyy/MM but i come to know that DatePicker control supports only two formats, short and long so i searched a way around to achieve this.

Solution:

I found on StackOverFlow that you can override the date patteren in you App.xaml.cs, as DatePicker gets the date format according to application culture. So solution is quite simple not as i thought it would be, it requires two steps.

  1. Go to the properties window of DatePicker and set its format to be short.
  2. Go to you App.xaml.cs and add below code inside its constructor:

//     For DatePicker custom date format

Thread.CurrentThread.CurrentCulture = (CultureInfo) Thread.CurrentThread.CurrentCulture.Clone();
            Thread.CurrentThread.CurrentCulture = (CultureInfo) Thread.CurrentThread.CurrentCulture.Clone();
            Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "yyyy/MM";

As our DatePicker is getting the Short date format so in last line we change it to our need.

Popular Posts