Mega Code Archive

 
Categories / C# / Date Time
 

Converts DateTimeOffset to a DateTimeOffset value representing the Coordinated Universal Time (UTC)

using System; using System.Globalization; public class Example {    public static void Main()    {         DateTimeOffset localTime, otherTime, universalTime;                  // Define local time in local time zone         localTime = new DateTimeOffset(new DateTime(2007, 6, 15, 12, 0, 0));         Console.WriteLine("Local time: {0}", localTime);                  // Convert local time to offset 0 and assign to otherTime         otherTime = localTime.ToOffset(TimeSpan.Zero);         Console.WriteLine("Other time: {0}", otherTime);         Console.WriteLine("{0} = {1}: {2}", localTime, otherTime, localTime.Equals(otherTime));         Console.WriteLine("{0} exactly equals {1}: {2}", localTime, otherTime, localTime.EqualsExact(otherTime));                  // Convert other time to UTC         universalTime = localTime.ToUniversalTime();          Console.WriteLine("Universal time: {0}", universalTime);         Console.WriteLine("{0} = {1}: {2}", otherTime, universalTime, universalTime.Equals(otherTime));         Console.WriteLine("{0} exactly equals {1}: {2}", otherTime, universalTime, universalTime.EqualsExact(otherTime));    } }