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
Selected Reading
DateTimeOffset.Compare() Method in C#
The DateTimeOffset.Compare() method in C# is used to compare two DateTimeOffset objects and indicates whether the first is earlier than the second, equal to the second, or later than the second. It returns an integer value,
- <0 − If val1 is earlier than val2
- 0 − If val1 is the same as val2
- >0 − If val1 is later than val2
Syntax
Following is the syntax −
public static int Compare (DateTimeOffset val1, DateTimeOffset val1);
Above, val1 is the 1st object to compare, whereas val2 is the 2nd.
Example
Let us now see an example to implement the DateTimeOffset.Compare() method −
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset1 = new DateTimeOffset(2019, 09, 09, 8, 20, 10, new TimeSpan(-5, 0, 0));
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(2019, 10, 09, 8, 20, 10, new TimeSpan(-5, 0, 0));
Console.WriteLine("DateTimeOffset1 = {0}", dateTimeOffset1);
Console.WriteLine("DateTimeOffset2 = {0}", dateTimeOffset2);
int res = DateTimeOffset.Compare(dateTimeOffset1, dateTimeOffset2);
// returns <0 since DateTimeOffset1 is earlier than DateTimeOffset2
Console.WriteLine(res);
}
}
Output
This will produce the following output −
DateTimeOffset1 = 9/9/2019 8:20:10 AM -05:00 DateTimeOffset2 = 10/9/2019 8:20:10 AM -05:00 -1
Example
Let us now see another example to implement the DateTimeOffset.Compare() method −
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset1 = new DateTimeOffset(2019, 09, 09, 8, 20, 10, new TimeSpan(-5, 0, 0));
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(2019, 09, 09, 8, 20, 10, new TimeSpan(-5, 0, 0));
Console.WriteLine("DateTimeOffset1 = {0}", dateTimeOffset1);
Console.WriteLine("DateTimeOffset2 = {0}", dateTimeOffset2);
int res = DateTimeOffset.Compare(dateTimeOffset1, dateTimeOffset2);
// returns 0 since DateTimeOffset1 is equal to DateTimeOffset2
Console.WriteLine(res);
}
}
Output
This will produce the following output −
DateTimeOffset1 = 9/9/2019 8:20:10 AM -05:00 DateTimeOffset2 = 9/9/2019 8:20:10 AM -05:00 0
Advertisements
