Mega Code Archive

 
Categories / C# / Date Time
 

Compare two DateTimeOffset values for time and offset

using System; using System.Globalization; public class Test {    public static void Main()    {         DateTimeOffset instanceTime = new DateTimeOffset(2007, 10, 31, 0, 0, 0, DateTimeOffset.Now.Offset);                  DateTimeOffset otherTime = instanceTime;         Console.WriteLine("{0} = {1}: {2}", instanceTime, otherTime, instanceTime.EqualsExact(otherTime));                  otherTime = new DateTimeOffset(instanceTime.DateTime, TimeSpan.FromHours(instanceTime.Offset.Hours + 1));         Console.WriteLine("{0} = {1}: {2}", instanceTime, otherTime, instanceTime.EqualsExact(otherTime));                  otherTime = new DateTimeOffset(instanceTime.DateTime + TimeSpan.FromHours(1), TimeSpan.FromHours(instanceTime.Offset.Hours + 1));         Console.WriteLine("{0} = {1}: {2}", instanceTime, otherTime,instanceTime.EqualsExact(otherTime));    } }