w3resource

C#: Convert current DateTime object to local time

C# Sharp DateTime: Exercise-31 with Solution

Write a C# Sharp program to convert the current DateTime object value to local time.

Sample Solution:-

C# Sharp Code:

using System;

class Example31
{
    static void Main()
    {
        // Declare variables to hold local and universal DateTime values
        DateTime localDateTime, univDateTime;

        // Prompt the user to enter a date and time
        Console.WriteLine("Enter a date and time.");
        string strDateTime = Console.ReadLine();

        try {
            // Parse the user input string to obtain a local DateTime object
            localDateTime = DateTime.Parse(strDateTime);

            // Convert the local DateTime to universal time
            univDateTime = localDateTime.ToUniversalTime();

            // Display the converted times to the user
            Console.WriteLine("{0} local time is {1} universal time.",
                                localDateTime,
                                univDateTime);
        }
        catch (FormatException) {
            // Handle invalid format input
            Console.WriteLine("Invalid format.");
            return;
        }

        // Prompt the user to enter a date and time in universal time
        Console.WriteLine("Enter a date and time in universal time.");
        strDateTime = Console.ReadLine();

        try {
            // Parse the user input string to obtain a universal DateTime object
            univDateTime = DateTime.Parse(strDateTime);

            // Convert the universal DateTime to local time
            localDateTime = univDateTime.ToLocalTime();

            // Display the converted times to the user
            Console.WriteLine("{0} universal time is {1} local time.",
                                univDateTime,
                                localDateTime);
        }
        catch (FormatException) {
            // Handle invalid format input
            Console.WriteLine("Invalid format.");
            return;
        }
    }
}

Sample Output:

Enter a date and time.                                                                                        
10.06.2017                                                                                                    
10/6/2017 12:00:00 AM local time is 10/5/2017 6:30:00 PM universal time.                                      
Enter a date and time in universal time.                                                                      
10.06.2017                                                                                                    
10/6/2017 12:00:00 AM universal time is 10/6/2017 5:30:00 AM local time.

Flowchart:

Flowchart: C# Sharp Exercises - Converts current DateTime object to local time

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to get the difference between two dates in days.
Next: Write a C# Sharp program to convert the value of the current DateTime object to its equivalent long date string representation.

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-31.php