w3resource

C#: Converts current DateTime object to UTC

C# Sharp DateTime: Exercise-40 with Solution

Write a C# Sharp program to convert the current DateTime object to Coordinated Universal Time (UTC).

Sample Solution:-

C# Sharp Code:

using System;

class Example40
{
    static void Main()
    {
        // Declare variables to store 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 {
            // Attempt to parse the entered string to a local DateTime.
            localDateTime = DateTime.Parse(strDateTime);
            // Convert the local DateTime to its equivalent Universal Time.
            univDateTime = localDateTime.ToUniversalTime();

            // Display the local and converted universal times.
            Console.WriteLine("{0} local time is {1} universal time.",
                                localDateTime,
                                univDateTime);
        }
        catch (FormatException) {
            // Catch a FormatException if the input string is not in a valid DateTime format.
            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 {
            // Attempt to parse the entered string to a universal DateTime.
            univDateTime = DateTime.Parse(strDateTime);
            // Convert the universal DateTime to its equivalent Local Time.
            localDateTime = univDateTime.ToLocalTime();

            // Display the universal and converted local times.
            Console.WriteLine("{0} universal time is {1} local time.",
                                     univDateTime,
                                     localDateTime);
        }
        catch (FormatException) {
            // Catch a FormatException if the input string is not in a valid DateTime format.
            Console.WriteLine("Invalid format.");
            return;
        }

    }
}

Sample Output:

Enter a date and time.                                                                                        
06/12/2017 02:20                                                                                              
6/12/2017 2:20:00 AM local time is 6/11/2017 8:50:00 PM universal time.                                       
Enter a date and time in universal time.                                                                      
06/12/2017 2:30                                                                                               
6/12/2017 2:30:00 AM universal time is 6/12/2017 8:00:00 AM local time.

Flowchart:

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

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to use each of the standard date time format strings to display the string representation of a date and time for four different cultures.
Next: Write a C# Sharp program to convert the specified string representation of a date and time to its DateTime equivalent.

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://www.w3resource.com/csharp-exercises/datetime/csharp-datetime-exercise-40.php