w3resource

C#: 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
{
    // Main method, entry point of the program
    public static void Main()
    {
        // Get the current local date and time
        DateTime localDate = DateTime.Now;

        // Get the current UTC (Coordinated Universal Time) date and time
        DateTime utcDate = DateTime.UtcNow;

        // Array of culture names
        String[] cultureNames = { "en-IE", "en-ZA", "fr-CA", "de-CH", "ro-RO" };

        // Iterate through each culture name in the array
        foreach (var cultureName in cultureNames)
        {
            // Create a CultureInfo object for the current culture name
            var culture = new CultureInfo(cultureName);

            // Display the native name of the culture
            Console.WriteLine("{0}:", culture.NativeName);

            // Display the local date and time in the current culture format
            // and show the 'Kind' of the DateTime object (local or UTC)
            Console.WriteLine("   Local date and time: {0}, {1:G}",
                              localDate.ToString(culture), localDate.Kind);

            // Display the UTC date and time in the current culture format
            // and show the 'Kind' of the DateTime object (always UTC)
            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 

Flowchart :

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

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to display the number of days of the year between two specified years.
Next: Write a C# Sharp program to display the number of ticks that have elapsed since the beginning of the twenty-first century and to instantiate a TimeSpan object using the Ticks property.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/csharp-exercises/datetime/csharp-datetime-exercise-5.php