w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Convert current DateTime object to its equivalent string representation of current culture

Secondary Nav

C# Sharp DateTime : Exercise-36 with Solution

Write a C# Sharp program to convert the value of the current DateTime object to its equivalent string representation using the formatting conventions of the current culture.

Expected Output :

5/16/2016 6:32:06 PM                                                             
16/05/2016 18:32:06                                                              
2016/05/16 18:32:06              

C# Sharp Code:

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

public class Example36
{
   public static void Main()
   {
      CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
      DateTime exampleDate = new DateTime(2016, 5, 16, 18, 32, 6);

      // Display the date using the current (en-US) culture.
      Console.WriteLine(exampleDate.ToString());

      // Change the current culture to fr-FR and display the date.
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
      Console.WriteLine(exampleDate.ToString());

      // Change the current culture to ja-JP and display the date.
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("ja-JP");
      Console.WriteLine(exampleDate.ToString());

      // Restore the original culture
      Thread.CurrentThread.CurrentCulture = currentCulture;
   }
}

C# Sharp Practice online:

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

public class Example36
{
   public static void Main()
   {
      CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
      DateTime exampleDate = new DateTime(2016, 5, 16, 18, 32, 6);

      // Display the date using the current (en-US) culture.
      Console.WriteLine(exampleDate.ToString());

      // Change the current culture to fr-FR and display the date.
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
      Console.WriteLine(exampleDate.ToString());

      // Change the current culture to ja-JP and display the date.
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("ja-JP");
      Console.WriteLine(exampleDate.ToString());

      // Restore the original culture
      Thread.CurrentThread.CurrentCulture = currentCulture;
   }
}

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:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number