w3resource logo
C# Sharp Programming Exercises

C# Sharp Exercises: String representation of a date and time for four different cultures

C# Sharp DateTime: Exercise-39 with Solution

Write a C# Sharp program to use each of the standard date time format strings to display the string representation of a date and time for four different cultures.

Sample Solution:-

C# Sharp Code:

using System;
using System.Globalization;

public class Example39
{
   public static void Main()
   {
      // Create an array of all supported standard date and time format specifiers.
      string[] formats = {"d", "D", "f", "F", "g", "G", "m", "o", "r", 
                          "s", "t", "T", "u", "U", "Y"};
      // Create an array of four cultures.                                 
      CultureInfo[] cultures = {CultureInfo.CreateSpecificCulture("zh-HK"), 
                                CultureInfo.CreateSpecificCulture("en-US"), 
                                CultureInfo.CreateSpecificCulture("es-GB"), 
                                CultureInfo.CreateSpecificCulture("fr-CA")};
       // Define date to be displayed.
      DateTime dateToDisplay = new DateTime(2015, 10, 16, 17, 4, 32);

      // Iterate each standard format specifier.
      foreach (string formatSpecifier in formats)
      {
         foreach (CultureInfo culture in cultures)
            Console.WriteLine("{0} Format Specifier {1, 10} Culture {2, 40}", 
                              formatSpecifier, culture.Name, 
                              dateToDisplay.ToString(formatSpecifier, culture));
         Console.WriteLine();
      }   
   }
}

Sample Output:

d Format Specifier      zh-HK Culture                               16/10/2015                                
d Format Specifier      en-US Culture                               10/16/2015                                
d Format Specifier      es-ES Culture                               16/10/2015                                
d Format Specifier      fr-CA Culture                               2015-10-16                                
                                                                                                              
D Format Specifier      zh-HK Culture                              2015年10月16日                                
D Format Specifier      en-US Culture                 Friday, October 16, 2015                                
D Format Specifier      es-ES Culture           viernes, 16 de octubre de 2015                                
D Format Specifier      fr-CA Culture                          16 octobre 2015 
------
d Format Specifier      zh-HK Culture                               16/10/2015                                
d Format Specifier      en-US Culture                               10/16/2015                                
d Format Specifier      es-ES Culture                               16/10/2015                                
d Format Specifier      fr-CA Culture                               2015-10-16                                
                                                                                                              
D Format Specifier      zh-HK Culture                              2015年10月16日                                
D Format Specifier      en-US Culture                 Friday, October 16, 2015                                
D Format Specifier      es-ES Culture           viernes, 16 de octubre de 2015                                
D Format Specifier      fr-CA Culture                          16 octobre 2015 

C# Sharp Code Editor:

using System;
using System.Globalization;

public class Example39
{
   public static void Main()
   {
      // Create an array of all supported standard date and time format specifiers.
      string[] formats = {"d", "D", "f", "F", "g", "G", "m", "o", "r", 
                          "s", "t", "T", "u", "U", "Y"};
      // Create an array of four cultures.                                 
      CultureInfo[] cultures = {CultureInfo.CreateSpecificCulture("zh-HK"), 
                                CultureInfo.CreateSpecificCulture("en-US"), 
                                CultureInfo.CreateSpecificCulture("es-GB"), 
                                CultureInfo.CreateSpecificCulture("fr-CA")};
       // Define date to be displayed.
      DateTime dateToDisplay = new DateTime(2015, 10, 16, 17, 4, 32);

      // Iterate each standard format specifier.
      foreach (string formatSpecifier in formats)
      {
         foreach (CultureInfo culture in cultures)
            Console.WriteLine("{0} Format Specifier {1, 10} Culture {2, 40}", 
                              formatSpecifier, culture.Name, 
                              dateToDisplay.ToString(formatSpecifier, culture));
         Console.WriteLine();
      }   
   }
}

Improve this sample solution and post your code through Disqus