C# Program to Get the Universal Time

In this article, find the C# Program to Get the Universal Time.


C# Program to Get the Universal Time

Here is an example of a C# program that gets the universal time and displays it on the console:


using System;

class UniversalTime
{
    static void Main(string[] args)
    {
        DateTime currentTime = DateTime.UtcNow;
        Console.WriteLine("The current universal time is: " + currentTime);
    }
}

Here is an another example of a .NET program that converts an ASCII string to a Hexadecimal string:

using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        string input = "Hello World!";
        string output = StringToHex(input);
        Console.WriteLine("Hexadecimal representation of '{0}': {1}", input, output);
        Console.ReadLine();
    }

    static string StringToHex(string input)
    {
        return String.Concat(input.Select(c => ((int)c).ToString("X2")));
    }
}

This program uses the DateTime class’s UtcNow property to get the current universal time, and then uses the Console class’s WriteLine method to display the time on the console.

You can also format the output using ToString() method

Console.WriteLine("The current universal time is: " + currentTime.ToString("yyyy-MM-dd HH:mm:ss"));

This will give the output in Year-Month-Day Hour:Minutes:Seconds format.


– Article ends here –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Share this:
We will be happy to hear your thoughts

      Leave a reply

      www.troubleshootyourself.com
      Logo