How to set custom format for DateTimePicker based on current culture

This code snippet shows how to set the custom format string for the DateTimePicker control based on current local settings and current culture.

The DateTimePicker control coming with Visual Studio allows you to specify a default date or time format. However, if you want to use the DateTimePicker control to show both date and time at the same time, you have to use a custom format string as shown below:

dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MM/dd/yyyy";

One of the problems in the above code is, the format is not culture specific. Since the localized settings and current culture is not taken into consideration, the code may not work as you expect for multiple culture, countries and regions.

The below code shows how to set a custom format string for DateTimePciker control which is localized for current culture.

CultureInfo culture = CultureInfo.CurrentCulture;
DateTimeFormatInfo info = culture.DateTimeFormat;

string datePattern = info.ShortDatePattern;

string timePattern = info.ShortTimePattern;

dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = datePattern + " " + timePattern;

More articles: DateTimePicker


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: