w3resource logo
C# Sharp Programming Exercises

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

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.

Sample Solution:-

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);
      }
   }
}

Sample Output:

English (Ireland):                                                                                            
   Local date and time: 09/06/2017 18:02:13, Local                                                            
   UTC date and time: 09/06/2017 12:32:13, Utc                                                                
      
English (South Africa):                                                                                       
   Local date and time: 2017-06-09 06:02:13 PM, Local                                                         
   UTC date and time: 2017-06-09 12:32:13 PM, Utc                                                             
    
français (Canada):                                                                                            
   Local date and time: 2017-06-09 18:02:13, Local                                                            
   UTC date and time: 2017-06-09 12:32:13, Utc                                                                
     
Deutsch (Schweiz):                                                                                            
   Local date and time: 09.06.2017 18:02:13, Local                                                            
   UTC date and time: 09.06.2017 12:32:13, Utc                                                                
      
română (România):                                                                                             
   Local date and time: 09.06.2017 18:02:13, Local                                                            
   UTC date and time: 09.06.2017 12:32:13, Utc 

C# Sharp Code Editor:

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