w3resource

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

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.



Share this Tutorial / Exercise on : Facebook and Twitter