How to get default date and time format string for the current culture?

This code snippet explains how to retrieve the default date and time format string from the Windows localization settings.

The DateTime object in .NET has several methods that allow you to format the date and time in various formats including short date format, long date format etc. Some examples are:

string shortDate = DateTime.Now.ToShortDateString();
string longDate = DateTime.Now.ToLongDateString();
string shortTime = DateTime.Now.ToShortTimeString();
string longTime = DateTime.Now.ToLongTimeString();

The above methods will output the date & time in current culture format, depending on the culture settings in the Windows operating system.

When you use custom format string, you will get the output in the format you specify, no matter what the current culture or local settings are.

There may be certain cases where you want to use the custom format string for datetime, but still you want to be aware of the current culture settings. The following code shows how to retrieve the DateTime format string based on the current culture settings and default locale of windows. This will be helpful when you work on localization projects.

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

string datePattern = info.ShortDatePattern;

string timePattern = info.ShortTimePattern;

More articles: DateTime Format


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: