DateTime.CompareTo() Method in C#
Last Updated :
22 Jan, 2019
This method is used to compare the value of this instance to a specified DateTime value and indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.
There are two methods in the overload list of this method as follows:
- CompareTo(DateTime)
- CompareTo(Object)
CompareTo(DateTime) Method
This method is used to compare 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.
Syntax:
public int CompareTo (DateTime value);
Here, the parameter
value is the object to compare to the current instance.
Return Value: This method return a signed number indicating the relative values of this instance and the value parameter.
- Less than zero : If this instance is earlier than value.
- Zero : If this instance is the same as value.
- Greater than zero : If this instance is later than value.
Example:
csharp
// C# code to demonstrate
// CompareTo(DateTime) function
using System;
public class GFG {
// Main Method
public static void Main()
{
// Taking two DateTime Objects
DateTime d1 = new DateTime(2018, 12,
31, 16, 0, 0);
DateTime d2 = new DateTime(2018, 12,
31, 20, 0, 0);
// Using the method
int res = d1.CompareTo(d2);
string r;
if (res > 0)
r = "is later than";
else if (res == 0)
r = "is the same time as";
else
r = "is earlier than";
Console.WriteLine("{0} {1} {2}", d1, r, d2);
}
}
Output:
12/31/2018 16:00:00 is earlier than 12/31/2018 20:00:00
CompareTo(Object) Method
This method is used to compare the value of this instance to a specified object that contains 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.
Syntax:
public int CompareTo (object value);
Here, the parameter
value is the boxed object to compare, or null.
Return Value: This method return a signed number indicating the relative values of this instance and the value parameter.
- Less than zero : If this instance is earlier than value.
- Zero : If this instance is the same as value.
- Greater than zero : If this instance is later than value.
Exception: This method will give
ArgumentException if the
value is not a DateTime.
Example:
csharp
// C# code to demonstrate
// CompareTo(Object) function
using System;
public class GFG {
// Main Method
public static void Main()
{
// Taking a DateTime Object
DateTime d1 = new DateTime(2018, 12,
31, 16, 0, 0);
// Using the method
// Here, "DateTime.Today" is
// the property of DateTime struct
int res = d1.CompareTo(DateTime.Today);
string r;
if (res > 0)
r = "is later than";
else if (res == 0)
r = "is the same time as";
else
r = "is earlier than";
Console.WriteLine("{0} {1} {2}", d1, r,
DateTime.Today);
}
}
Output:
12/31/2018 16:00:00 is earlier than 01/21/2019 00:00:00
Reference:
Similar Reads
DateTime.Compare() Method in C# This method is used to compare two instances of DateTime and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance. Syntax: public static int Compare (DateTime t1, DateTime t2); Parameters: t1: The first object to compare. t2: Th
2 min read
DateTimeOffset.CompareTo() Method in C# DateTimeOffset.CompareTo(DateTimeOffset) Method is used to compare the current DateTimeOffset object to a specified DateTimeOffset object and indicates whether the current object is earlier than, the same as, or later than the second DateTimeOffset object. Syntax: public int CompareTo (DateTimeOffse
2 min read
DateTimeOffset.Compare() Method in C# DateTimeOffset.Compare(DateTimeOffset, DateTimeOffset) Method is used to compare two DateTimeOffset objects and shows whether the first is earlier than the second, equal to the second, or later than the second. Syntax: public static int Compare (DateTimeOffset first, DateTimeOffset second); Paramete
2 min read
Decimal.CompareTo() Method in C# This method is used to compare the current instance to a specified object or Decimal and returns an indication of their relative values. There are 2 methods in the overload list of this method as follows: CompareTo(Decimal) MethodCompareTo(Object) MethodDecimal.CompareTo(Decimal) Method This method
4 min read
Decimal.Compare() Method in C# This method is used to compare two specified Decimal values. Syntax: public static int Compare (decimal a1, decimal a2); Parameters: a1:This parameter specifies the first value to compare. a2:This parameter specifies the second value to compare. Return Value: It returns a signed number indicating th
2 min read