w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Current date and time on the local computer

Secondary Nav

C# Sharp DateTime : Exercise-5 with Solution

Write a C# Sharp program to get a DateTime value that represents the current date and time on the local computer.

Expected Output :

English (Ireland):                                                      
   Local date and time: 20/08/2016 15:49:03, Local                      
   UTC date and time: 20/08/2016 10:19:03, Utc                          
                                                                        
English (South Africa):                                                 
   Local date and time: 2016-08-20 03:49:03 PM, Local                   
   UTC date and time: 2016-08-20 10:19:03 AM, Utc                       
                                                                        
français (Canada):                                                      
   Local date and time: 2016-08-20 15:49:03, Local                      
   UTC date and time: 2016-08-20 10:19:03, Utc                          
                                                                        
Deutsch (Schweiz):                                                      
   Local date and time: 20.08.2016 15:49:03, Local                      
   UTC date and time: 20.08.2016 10:19:03, Utc                          
                                                                        
română (România):                                                       
   Local date and time: 20.08.2016 15:49:03, Local
   UTC date and time: 20.08.2016 10:19:03, Utc

C# Sharp Code:

 using System;
using System.Globalization;
public class Example5
{
   public static void Main()
   {
      DateTime localDate = DateTime.Now;
      DateTime utcDate = DateTime.UtcNow;
      String[] cultureNames = { "en-IE", "en-ZA", "fr-CA",
                                "de-CH", "ro-RO" } ;

      foreach (var cultureName in cultureNames) {
         var culture = new CultureInfo(cultureName);
         Console.WriteLine("{0}:", culture.NativeName);
         Console.WriteLine("   Local date and time: {0}, {1:G}",
                           localDate.ToString(culture), localDate.Kind);
         Console.WriteLine("   UTC date and time: {0}, {1:G}\n",
                           utcDate.ToString(culture), utcDate.Kind);
      }
   }
}

C# Sharp Practice online:

using System;
using System.Globalization;
public class Example5
{
   public static void Main()
   {
      DateTime localDate = DateTime.Now;
      DateTime utcDate = DateTime.UtcNow;
      String[] cultureNames = { "en-IE", "en-ZA", "fr-CA",
                                "de-CH", "ro-RO" } ;

      foreach (var cultureName in cultureNames) {
         var culture = new CultureInfo(cultureName);
         Console.WriteLine("{0}:", culture.NativeName);
         Console.WriteLine("   Local date and time: {0}, {1:G}",
                           localDate.ToString(culture), localDate.Kind);
         Console.WriteLine("   UTC date and time: {0}, {1:G}\n",
                           utcDate.ToString(culture), utcDate.Kind);
      }
   }
}

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