w3resource logo
C# Sharp Programming Exercises

C# Sharp Exercises: Convert current DateTime object to its equivalent short date string representation

C# Sharp DateTime: Exercise-34 with Solution

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

Sample Solution:-

C# Sharp Code:

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

public class Example34
{
   public static void Main()
   {
      DateTime dateToDisplay = new DateTime(2016, 8, 17, 8, 42, 50);
      CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
      // Change culture to en-NZ.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-NZ");
      Console.WriteLine("Displaying short date for {0} culture:", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0} (Short Date String)", 
                        dateToDisplay.ToShortDateString());
      // Display using 'd' standard format specifier to illustrate it is
      // identical to the string returned by ToShortDateString.
      Console.WriteLine("   {0} ('d' standard format specifier)", 
                        dateToDisplay.ToString("d"));
      Console.WriteLine();

      // Change culture to fi-FI.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fi-FI");
      Console.WriteLine("Displaying short date for {0} culture:", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0}", dateToDisplay.ToShortDateString());
      Console.WriteLine();

      // Change culture to de-CH.    
      Thread.CurrentThread.CurrentCulture = new CultureInfo("de-CH");
      Console.WriteLine("Displaying short date for {0} culture:", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0}", dateToDisplay.ToShortDateString());

      // Restore original culture.
      Thread.CurrentThread.CurrentCulture = originalCulture;
   }
}

Sample Output:

Displaying short date for en-NZ culture:                                                                      
   17/08/2016 (Short Date String)                                                                             
   17/08/2016 ('d' standard format specifier)                                                                  
Displaying short date for fi-FI culture:                                                                      
   17.8.2016                                                                                                      
Displaying short date for de-CH culture:                                                                      
   17.08.2016

C# Sharp Code Editor:

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

public class Example34
{
   public static void Main()
   {
      DateTime dateToDisplay = new DateTime(2016, 8, 17, 8, 42, 50);
      CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
      // Change culture to en-NZ.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-NZ");
      Console.WriteLine("Displaying short date for {0} culture:", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0} (Short Date String)", 
                        dateToDisplay.ToShortDateString());
      // Display using 'd' standard format specifier to illustrate it is
      // identical to the string returned by ToShortDateString.
      Console.WriteLine("   {0} ('d' standard format specifier)", 
                        dateToDisplay.ToString("d"));
      Console.WriteLine();

      // Change culture to fi-FI.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fi-FI");
      Console.WriteLine("Displaying short date for {0} culture:", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0}", dateToDisplay.ToShortDateString());
      Console.WriteLine();

      // Change culture to de-CH.    
      Thread.CurrentThread.CurrentCulture = new CultureInfo("de-CH");
      Console.WriteLine("Displaying short date for {0} culture:", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0}", dateToDisplay.ToShortDateString());

      // Restore original culture.
      Thread.CurrentThread.CurrentCulture = originalCulture;
   }
}

Improve this sample solution and post your code through Disqus