CSharp - Write program to Get Current Date Time In Universal Time Coordinated (UTC)


You will need to get the current date and time in UTC.


You can use DateTimeOffset that contain time zone information in addition to the date and time.


using System;
class Program// w ww . j  a  va  2 s . co  m
    static void Main(string[] args)
        // Current time serves as input 
        DateTime now = DateTime.Now;
        DateTime utcNow = DateTime.UtcNow;
        DateTimeOffset completeInstant = DateTimeOffset.Now;
        DateTimeOffset utcCompleteInstant = DateTimeOffset.UtcNow;

        // Outputs 
        Console.WriteLine("Now: " + now);
        Console.WriteLine("UTC now: " + utcNow);
        Console.WriteLine("Now (including time zone): " + completeInstant);
        Console.WriteLine("Time zone (offset against UTC): " + completeInstant.Offset.TotalHours);
        Console.WriteLine("UTC now (including time zone): " + utcCompleteInstant);