w3resource

C#: 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 current DateTime object value 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()
   {
      // Define a DateTime object to display.
      DateTime dateToDisplay = new DateTime(2016, 8, 17, 8, 42, 50);

      // Store the original culture of the current thread.
      CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;

      // Change culture to en-NZ (New Zealand).
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-NZ");
      Console.WriteLine("Displaying short date for {0} culture:", 
                        Thread.CurrentThread.CurrentCulture.Name);
      // Display short date using ToShortDateString().
      Console.WriteLine("   {0} (Short Date String)", 
                        dateToDisplay.ToShortDateString());
      // Display using 'd' standard format specifier to illustrate its identity with ToShortDateString.
      Console.WriteLine("   {0} ('d' standard format specifier)", 
                        dateToDisplay.ToString("d"));
      Console.WriteLine();

      // Change culture to fi-FI (Finnish - Finland).
      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 (German - Switzerland).    
      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 the original culture of the current thread.
      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

Flowchart :

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

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.