w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: String representation of a date and time using CultureInfo objects

Secondary Nav

C# Sharp DateTime : Exercise-37 with Solution

Write a C# Sharp program to display the string representation of a date and time using CultureInfo objects that represent five different cultures.

Expected Output :

In Invariant Language (Invariant Country), 05/17/2016 09:00:00                   
In en-ZA, 2016-05-17 09:00:00 AM                                                 
In ko-KR, 2016-05-17 오전 9:00:00                                                  
In de-DE, 17.05.2016 09:00:00                                                    
In es-ES, 17/05/2016 9:00:00                                                     
In en-US, 5/17/2016 9:00:00 AM          

C# Sharp Code:

using System;
using System.Globalization;

public class Example37
{
   public static void Main()
   {
      CultureInfo[] cultures = new CultureInfo[] {CultureInfo.InvariantCulture, 
                                                  new CultureInfo("en-ZA"), 
                                                  new CultureInfo("ko-KR"), 
                                                  new CultureInfo("de-DE"), 
                                                  new CultureInfo("es-ES"),
                                                  new CultureInfo("en-US")};

      DateTime thisDate = new DateTime(2016, 5, 17, 9, 0, 0);                                            

      foreach (CultureInfo culture in cultures)
      {
         string cultureName; 
         if (string.IsNullOrEmpty(culture.Name))
            cultureName = culture.NativeName;
         else
            cultureName = culture.Name;

         Console.WriteLine("In {0}, {1}", 
                           cultureName, thisDate.ToString(culture));
      }                                            
      }
}

C# Sharp Practice online:

using System;
using System.Globalization;

public class Example37
{
   public static void Main()
   {
      CultureInfo[] cultures = new CultureInfo[] {CultureInfo.InvariantCulture, 
                                                  new CultureInfo("en-ZA"), 
                                                  new CultureInfo("ko-KR"), 
                                                  new CultureInfo("de-DE"), 
                                                  new CultureInfo("es-ES"),
                                                  new CultureInfo("en-US")};

      DateTime thisDate = new DateTime(2016, 5, 17, 9, 0, 0);                                            

      foreach (CultureInfo culture in cultures)
      {
         string cultureName; 
         if (string.IsNullOrEmpty(culture.Name))
            cultureName = culture.NativeName;
         else
            cultureName = culture.Name;

         Console.WriteLine("In {0}, {1}", 
                           cultureName, thisDate.ToString(culture));
      }                                            
      }
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program