w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Convert the value of the current DateTime object to its equivalent long time string representation

Secondary Nav

C# Sharp DateTime : Exercise-33 with Solution

Write a C# Sharp program to convert the value of the current DateTime object to its equivalent long time string representation.

Expected Output :

Current culture: en-CA                                                           
Date: 2016-08-17 10:30:15 AM                                                     
Long time pattern: 'h:mm:ss tt'                                                  
Long time with format string:     10:30:15 AM                                    
Long time with ToLongTimeString:  10:30:15 AM                                    
                                                                                 
Current culture: en-AU                                                           
Date: 17/08/2016 10:30:15 AM                                                     
Long time pattern: 'h:mm:ss tt'                                                  
Long time with format string:     10:30:15 AM                                    
Long time with ToLongTimeString:  10:30:15 AM                                    
                                                                                 
Current culture: fr-CH                                                           
Date: 17.08.2016 10:30:15                                                        
Long time pattern: 'HH:mm:ss'                                                    
Long time with format string:     10:30:15                                       
Long time with ToLongTimeString:  10:30:15     

Current culture: de-AT                                                           
Date: 17.08.2016 10:30:15                                                        
Long time pattern: 'HH:mm:ss'                                                    
Long time with format string:     10:30:15                                       
Long time with ToLongTimeString:  10:30:15   

C# Sharp Code:

using System;
using System.Threading;
using System.Globalization;

public class Example33
{
   public static void Main() 
   {
      // Create an array of culture names.
      String[] names = { "en-CA", "en-AU", "fr-CH", "de-AT" };
      // Initialize a DateTime object.
      DateTime dateValue = new System.DateTime(2016, 8, 17, 10, 30, 15);

      // Iterate the array of culture names.
      foreach (var name in names) {
         // Change the culture of the current thread.
         Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(name);
         // Display the name of the current culture and the date.
         Console.WriteLine("Current culture: {0}", CultureInfo.CurrentCulture.Name);
         Console.WriteLine("Date: {0:G}", dateValue);        

         // Display the long time pattern and the long time.
         Console.WriteLine("Long time pattern: '{0}'", 
                           DateTimeFormatInfo.CurrentInfo.LongTimePattern);
         Console.WriteLine("Long time with format string:     {0:T}", dateValue);
         Console.WriteLine("Long time with ToLongTimeString:  {0}\n", 
                           dateValue.ToLongTimeString());
      }
   }
}

C# Sharp Practice online:

using System;
using System.Threading;
using System.Globalization;

public class Example33
{
   public static void Main() 
   {
      // Create an array of culture names.
      String[] names = { "en-CA", "en-AU", "fr-CH", "de-AT" };
      // Initialize a DateTime object.
      DateTime dateValue = new System.DateTime(2016, 8, 17, 10, 30, 15);

      // Iterate the array of culture names.
      foreach (var name in names) {
         // Change the culture of the current thread.
         Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(name);
         // Display the name of the current culture and the date.
         Console.WriteLine("Current culture: {0}", CultureInfo.CurrentCulture.Name);
         Console.WriteLine("Date: {0:G}", dateValue);        

         // Display the long time pattern and the long time.
         Console.WriteLine("Long time pattern: '{0}'", 
                           DateTimeFormatInfo.CurrentInfo.LongTimePattern);
         Console.WriteLine("Long time with format string:     {0:T}", dateValue);
         Console.WriteLine("Long time with ToLongTimeString:  {0}\n", 
                           dateValue.ToLongTimeString());
      }
   }
}

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