Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 17 Nov 2003
Rate this:
Please Sign up or sign in to vote.
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.

using System.Globalization;

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

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.

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:

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.

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

Share

About the Author

Amol Jaysingpure
Web Developer
India India
No Biography provided

Comments and Discussions

 
GeneralGreat Example PinmemberMark Focas18-Nov-03 11:25 
QuestionIs there no "windows default locale" option? PinmemberJ Cardinal28-Feb-03 8:42 
AnswerRe: Is there no "windows default locale" option? PinmemberMark Conger28-Feb-03 9:46 
AnswerRe: Is there no &quot;windows default locale&quot; option? Pinmemberload123426-Nov-03 23:37 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 18 Nov 2003
Article Copyright 2003 by Amol Jaysingpure
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid