Compartir a través de


DateTime.CompareTo Method (DateTime)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Compares the value of this instance to a specified DateTime value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Function CompareTo ( _
    value As DateTime _
) As Integer
public int CompareTo(
    DateTime value
)

Parameters

Return Value

Type: System.Int32
A signed integer that indicates the relationship between this instance and the value parameter, as shown in the following table.

Value

Description

Less than zero

This instance is earlier than value.

Zero

This instance is the same as value.

Greater than zero

This instance is later than value.

Implements

IComparable<T>.CompareTo(T)

Remarks

This method implements the System.IComparable<T> interface and performs slightly better than the DateTime.CompareTo(Object) method overload because it does not have to convert the value parameter to an object.

Before comparing DateTime objects, make sure that the objects represent times in the same time zone. You can do this by comparing the values of their Kind properties.

Examples

The following example instantiates three DateTime objects, one that represents today's date, another that represents the date one year previously, and a third that represents the date one year in the future. It then calls the CompareTo(DateTime) method and displays the result of the comparison.

Option Strict On

Module Example
   Private Enum DateComparisonResult
      Earlier = -1
      Later = 1
      TheSame = 0
   End Enum

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim thisDate As Date = Date.Today

      ' Define two DateTime objects for today's date 
      ' next year and last year     
      Dim thisDateNextYear, thisDateLastYear As Date

      ' Call AddYears instance method to add/substract 1 year
      thisDateNextYear = thisDate.AddYears(1)
      thisDateLastYear = thisDate.AddYears(-1)


      ' Compare dates
      '
      Dim comparison As DateComparisonResult
      ' Compare today to last year
      comparison = CType(thisDate.CompareTo(thisDateLastYear), DateComparisonResult)
      outputBlock.Text += String.Format("CompareTo method returns {0}: {1:d} is {2} than {3:d}", _
                        CInt(comparison), thisDate, comparison.ToString().ToLower(), _
                        thisDateLastYear) + vbCrLf

      ' Compare today to next year
      comparison = CType(thisDate.CompareTo(thisDateNextYear), DateComparisonResult)
      outputBlock.Text += String.Format("CompareTo method returns {0}: {1:d} is {2} than {3:d}", _
                        CInt(comparison), thisDate, comparison.ToString().ToLower(), _
                        thisDateNextYear) + vbCrLf
   End Sub
End Module
'
' If run on October 20, 2006, the example produces the following output:
'    CompareTo method returns 1: 10/20/2006 is later than 10/20/2005
'    CompareTo method returns -1: 10/20/2006 is earlier than 10/20/2007
using System;

public class Example
{
   private enum DateComparisonResult
   {
      Earlier = -1,
      Later = 1,
      TheSame = 0
   };

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      DateTime thisDate = DateTime.Today;


      // Define two DateTime objects for today's date 
      // next year and last year        
      DateTime thisDateNextYear, thisDateLastYear;

      // Call AddYears instance method to add/substract 1 year
      thisDateNextYear = thisDate.AddYears(1);
      thisDateLastYear = thisDate.AddYears(-1);

      // Compare dates
      //
      DateComparisonResult comparison;
      // Compare today to last year
      comparison = (DateComparisonResult)thisDate.CompareTo(thisDateLastYear);
      outputBlock.Text += String.Format("CompareTo method returns {0}: {1:d} is {2} than {3:d}",
                        (int)comparison, thisDate, comparison.ToString().ToLower(),
                        thisDateLastYear) + "\n";

      // Compare today to next year
      comparison = (DateComparisonResult)thisDate.CompareTo(thisDateNextYear);
      outputBlock.Text += String.Format("CompareTo method returns {0}: {1:d} is {2} than {3:d}",
                        (int)comparison, thisDate, comparison.ToString().ToLower(),
                        thisDateNextYear) + "\n";
   }
}
//
// If run on October 20, 2006, the example produces the following output:
//    CompareTo method returns 1: 10/20/2006 is later than 10/20/2005
//    CompareTo method returns -1: 10/20/2006 is earlier than 10/20/2007

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.