Silverlight DatePicker - SelectedDateFormat ignore culture settings

by dsoltesz 2. February 2010 21:38 >

In my silverlight app I wanted my datepicker controls to always format dates to M/dd/yyyy date format strings regardless of the current users culture setting.

First I change my regional setting to be English(Ireland) and when I run my app, the datepicker will default to that cultures date format off dd/MM/yyyy

but this isn't what I want, I want it to be M/dd/yyyy. 

This can easliy be changed by adding some code to your app.xaml file

public App()

        {

            //force dates to always be M/dd/yyyy regardless of culture

            Thread.CurrentThread.CurrentCulture = (CultureInfo) Thread.CurrentThread.CurrentCulture.Clone();

            Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "M/dd/yyyy";

            Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern = "h:mm tt";

            InitializeComponent();

        }

 Now when you run your app, even though your regional settings are set to English (Ireland) the datepicker still has the selecteddateformat of M/dd/yyyyy

Tags: , , , , ,

c# | silverlight

about the author

My name is Dan Soltesz, and I"m a consultant specializing in Microsoft technologies

  • Silverlight 2.0,3.0,4.0,5.0
  • Windows Phone 7,8
  • Windows 8
  • WPF
  • MVC / SPA
  • ASP.NET 2.0,3.0,3.5,4.0
  • WCF, WCF Ria Services
  • Entity Framework / EF Code First
  • C#
  • MVVM, Repository Pattern

to name a few.  On this site you will find links to sites/blogs that I read and my own attempt at writing some blogs that may help others as well as a few miscellaneous items.  If there is something that you need to know about me and cannot find on this site, please feel free to contact me.

page list

sponsors

month list

recentcomments

Comment RSS