Compare DateTimeOffset

In this chapter you will learn:

  1. Compare two DateTimeOffset values for equality
  2. How to use EqualsExact method to compare DateTimeOffset
  3. Compare method

Compare two DateTimeOffset values for equality

using System;/*from   j a  va 2s.co  m*/
using System.Globalization;
public class Test
{
   public static void Main()
   {
       DateTimeOffset firstTime = new DateTimeOffset(2010, 9, 2, 6, 45, 0, new TimeSpan(-7, 0, 0));
    
       DateTimeOffset secondTime = firstTime;
       Console.WriteLine("{0} = {1}: {2}", firstTime, secondTime, firstTime.Equals(secondTime));
    
       secondTime = new DateTimeOffset(2010, 9, 2, 6, 45, 0, new TimeSpan(-6, 0, 0));      
       Console.WriteLine("{0} = {1}: {2}", firstTime, secondTime, firstTime.Equals(secondTime));
    
       secondTime = new DateTimeOffset(2010, 9, 2, 8, 45, 0, new TimeSpan(-5, 0, 0));
       Console.WriteLine("{0} = {1}: {2}", firstTime, secondTime, firstTime.Equals(secondTime));
   }
}

EqualsExact method

using System;//from j a v a2s  .  c o  m
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));
   }
}

Compare method

using System;//from j a  v a  2s.  com

public class CompareTimes
{
   public static void Main()
   {
      DateTimeOffset firstTime = new DateTimeOffset(2007, 9, 1, 6, 45, 0, new TimeSpan(-7, 0, 0));

      DateTimeOffset secondTime = firstTime;
      Console.WriteLine(DateTimeOffset.Compare(firstTime, secondTime));

      secondTime = new DateTimeOffset(2007, 9, 1, 6, 45, 0, new TimeSpan(-6, 0, 0));      
      Console.WriteLine(DateTimeOffset.Compare(firstTime, secondTime));

      secondTime = new DateTimeOffset(2007, 9, 1, 8, 45, 0, new TimeSpan(-5, 0, 0));
      Console.WriteLine(DateTimeOffset.Compare(firstTime, secondTime));
   }
}

Next chapter...

What you will learn in the next chapter:

  1. Create DateTimeOffset using the specified DateTime value.
  2. Create DateTimeOffset from ticks and offset
Home » C# Tutorial » Date, Time, TimeZone
Compare DateTimeOffset
DateTimeOffset creation
Date time Value from DateTimeOffset
Compare DateTime value
DateTime constant value
DateTime constructor
DateTime properties
Create TimeSpan From
TimeSpan
TimeSpan add and subtract
DateTime and TimeSpan
Compare TimeSpan
TimeSpan constant
TimeSpan Constructor
TimeSpan Duration
TimeSpan format
Negate a TimeSpan
TimeSpan operators
Parse string to TimeSpan
TimeSpan to string
TimeSpan total properties
TimeSpan propeties
Daylight saving
Time zone ID
UTC offset with TimeZone
TimeZone converting
TimeZone creation