Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#
Article

Use of different Date Time formats and Culture types by CultureInfo and DateTimeFormatInfo class

Rate me:
Please Sign up or sign in to vote.
2.09/5 (20 votes)
17 Nov 20032 min read 132.9K   877   22   4
Using System.Globalizaion.CultureInfo & System.Globalization.DateTimeFormatInfo classes.

Introduction

Globalization means the process of preparing your code to be able to support different languages. This is done by eliminating language or culture dependencies from your code to become culture-neutral. That is to avoid using hard coded strings or messages to be displayed to the user. .NET has a bunch of classes integrated to support the development of international software. These classes are located in the namespaces System.Globalization and System.Resources. CultureInfo is the class that holds information about a certain language, as formatting of numbers and dates, calendar to use, decimal character...etc.

Details

The Culture names follow the RFC 1766 standard in the format <languagecode>-<country/regioncode> where <languagecode> is a lowercase two-letter code derived form ISO639-1 and <country/regioncode> is an uppercase two-letter code derived from ISO 3166. The cultures are generally grouped into three sets:

  • Invariant culture:- It is associated with the English language but not with any country/region.
  • Neutral culture:- It is associated with language but not with the country/region code.
  • Specific culture :- It is associated with language and country/region code.

A DateTimeFormatInfo can be created only for the invariant culture or for specific culture, not for a neutral culture. DateTimeFormatInfo inherits from Object and implements ICloneable and IFormarProvider interfaces. For date and time, several standard patterns are defined with the default property values. We can also use the custom format using a set of format pattern characters. CultureInfo class encapsulates information about handling according to the special requirements of a particular culture and language. DateTime values are formatted by the ParseExact() and ToString() methods according to Custom or Standard patterns. The Standard format patterns and the Custom format patterns are as follows:

Format Specifier            Format Pattern

       d                    MM/dd/yyyy
       D                    dddd*, MMMM* dd, yyyy
       f                    dddd*, MMMM* dd, yyyy HH*:mm*
       F                    dddd*, MMMM* dd, yyyy HH*:mm*:ss*
       g                    MM/dd/yyyy HH*:mm*
       G                    MM/dd/yyyy HH*:mm*:ss*
       m, M                 MMMM* dd
       t                    HH*:mm*
       T                    HH*:mm*:ss*
       U                    dddd*, MMMM* dd, yyyy HH*:mm*:ss*
       y, Y                 YYYY MMMM*

About Source Code

In the enclosed source code, add the the Globalization namespace.

C#
using System.Globalization;

Now, collect all the culture with the following code of line:

C#
CultureInfo [] ci = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

CultureInfo.GetCulture gets the list of supported cultures filtered by the specified CultureType and it returns an array of type CultureInfo.

C#
cbCultures.Items.Add(new cultureinfo(ci[i].Name));

c[i].Name gets the Culture name in the Format “<languagecode>-<CountryCode>”. To change this to the required string, pass this to the cultureinfo class which is derived from CultureInfo class. cultureinfo class contains ToString() method to change the culture in the localized version of the .NET framework. Now collect all the date time format.

For example:

C#
patterns = dtf.GetAllDateTimePatterns('D'); 
for (int i = 0;i < patterns.GetLength(0);i++) 
    {
         updLongDatePattern.Items.Add(patterns[i]);
    }

where D is the Format specifier for the Long date pattern.

C#
private void updLongDatePattern_SelectedItemChanged(object sender, 
                                                 System.EventArgs e) 
{
    try 
    {
         textLongDate.Text = dt.ToString(updLongDatePattern.Text, 
                                                curci.DateTimeFormat);
    } 
    catch {}
}

Use the ToString() method for the given date which converts the value of this instance to its equivalent string representation using specified format and culture-specific format information.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGreat Example Pin
Mark Focas18-Nov-03 11:25
Mark Focas18-Nov-03 11:25 
QuestionIs there no "windows default locale" option? Pin
Member 9628-Feb-03 8:42
Member 9628-Feb-03 8:42 
AnswerRe: Is there no "windows default locale" option? Pin
Mark Conger28-Feb-03 9:46
Mark Conger28-Feb-03 9:46 
AnswerRe: Is there no &quot;windows default locale&quot; option? Pin
load123426-Nov-03 23:37
load123426-Nov-03 23:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.