11.7 Displaying Culture Aware Date and Time

 <  Day Day Up  >  

You want to ensure that you format any date and time information so that it appears correctly within a specific culture.


Technique

The CultureInfo class contains a DateTimeFormatInfo object used to format date and time information for a specific culture. Create a new CultureInfo object and use the DateTimeFormatInfo property of that object in any methods that utilize an IFormatProvider parameter. Also, just as you can use a format specifier to control how a number is displayed, various date and time format specifiers control how the date and time appears within that given culture. The following code shows how to display different date formats within a given culture using a CultureInfo 's DateTimeFormatInfo property and various format specifiers:

 
 currentDTFI = new CultureInfo("de-DE", false ).DateTimeFormat; Console.WriteLine(DateTime.Today.ToString("d", currentDTFI )); Console.WriteLine(DateTime.Today.ToString("D", currentDTFI )); Console.WriteLine(DateTime.Today.ToString("f", currentDTFI )); Console.WriteLine(DateTime.Today.ToString("F", currentDTFI )); 

Comments

Again, just as it isn't necessary to specify a NumberFormatInfo object to format a number using a user 's default culture, there is no need to specify a DateTimeFormatInfo object either. However, when you in fact need to display date and time information for a culture that is different from the CurrentCulture object within the CultureInfo class, the code in the "Technique" section allows you to do so.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net