Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
DateTime.Compare() Method in C#
The DateTime.Compare() method in C# is used for comparison of two DateTime instances. It returns an integer value indicating the relationship between the two dates −
- <0 − If date1 is earlier than date2
- 0 − If date1 is the same as date2
- >0 − If date1 is later than date2
Syntax
Following is the syntax for DateTime.Compare() method −
public static int Compare(DateTime d1, DateTime d2);
Parameters
-
d1 − The first
DateTimeinstance to compare -
d2 − The second
DateTimeinstance to compare
Return Value
The method returns an integer value −
- Negative integer − If d1 is earlier than d2
- Zero (0) − If d1 equals d2
- Positive integer − If d1 is later than d2
Using DateTime.Compare() for Earlier Date
The following example demonstrates comparing two dates where the first date is earlier than the second −
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2019, 11, 20, 6, 20, 40);
DateTime d2 = d1.AddYears(5);
Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1);
Console.WriteLine("New DateTime (adding years) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d2);
int res = DateTime.Compare(d1, d2);
// returns
The output of the above code is −
Initial DateTime = 20 November 2019, 06:20:40
New DateTime (adding years) = 20 November 2024, 06:20:40
Comparison result: -1
First date is earlier than second date
Using DateTime.Compare() for Equal Dates
The following example shows comparison when both dates are identical −
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2019, 11, 20, 6, 20, 40);
DateTime d2 = new DateTime(2019, 11, 20, 6, 20, 40);
Console.WriteLine("DateTime 1 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1);
Console.WriteLine("DateTime 2 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d2);
int res = DateTime.Compare(d1, d2);
// returns equal to 0 since d1 is equal to d2
Console.WriteLine("Comparison result: " + res);
if (res == 0) {
Console.WriteLine("Both dates are equal");
}
}
}
The output of the above code is −
DateTime 1 = 20 November 2019, 06:20:40
DateTime 2 = 20 November 2019, 06:20:40
Comparison result: 0
Both dates are equal
Using DateTime.Compare() for Later Date
The following example demonstrates when the first date is later than the second date −
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2024, 5, 15, 14, 30, 0);
DateTime d2 = new DateTime(2022, 3, 10, 9, 15, 30);
Console.WriteLine("DateTime 1 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1);
Console.WriteLine("DateTime 2 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d2);
int res = DateTime.Compare(d1, d2);
Console.WriteLine("Comparison result: " + res);
if (res > 0) {
Console.WriteLine("First date is later than second date");
}
}
}
The output of the above code is −
DateTime 1 = 15 May 2024, 02:30:00
DateTime 2 = 10 March 2022, 09:15:30
Comparison result: 1
First date is later than second date
Comparison with Other DateTime Methods
| Method | Return Type | Usage |
|---|---|---|
| DateTime.Compare() | int | Static method for comparing two DateTime instances |
| CompareTo() | int | Instance method to compare with another DateTime |
| Equals() | bool | Returns true if dates are equal, false otherwise |
Conclusion
The DateTime.Compare() method provides a reliable way to compare two DateTime instances. It returns negative, zero, or positive integers to indicate whether the first date is earlier, equal to, or later than the second date, making it essential for date sorting and conditional operations.
