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);
Parameters:
first: It is the first object to compare.
second: It is the second object to compare.Return Value: This method returns a signed integer that indicates whether the value of the first parameter is earlier than, later than or the same time as the value of the second parameter.
- Less than zero: Means first is earlier than second.
- Zero: Means first is equal to second.
- Greater than zero: Means first is later than second.
Below programs illustrate the use of DateTimeOffset.Compare(DateTimeOffset, DateTimeOffset) Method:
Example 1:
// C# program to demonstrate the // DateTimeOffset.Compare(DateTimeOffset, // DateTimeOffset) Method using System; using System.Globalization; class GFG { // Main Method public static void Main() { // creating object of DateTimeOffset DateTimeOffset offset1 = new DateTimeOffset(2007, 6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0)); // creating object of DateTimeOffset DateTimeOffset offset2 = new DateTimeOffset(2006, 6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0)); // comparing two offset1 and offset2 // instance using Compare() method int value = DateTimeOffset.Compare(offset1, offset2); if (value > 0) { Console.Write("offset1 is later than offset2 "); } else if (value < 0) { Console.Write("offset1 is earlier than offset2"); } else { Console.Write("offset1 is equal to offset2"); } } } |
offset1 is later than offset2
Example 2: For ArgumentOutOfRangeException
// C# program to demonstrate the // DateTimeOffset.Compare(DateTimeOffset, // DateTimeOffset) Method using System; using System.Globalization; class GFG { // Main Method public static void Main() { // creating object of DateTimeOffset DateTimeOffset offset1 = new DateTimeOffset(2006, 6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0)); // creating object of DateTimeOffset DateTimeOffset offset2 = new DateTimeOffset(2006, 6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0)); // comparing two offset1 and offset2 // instance using Compare() method int value = DateTimeOffset.Compare(offset1, offset2); if (value > 0) { Console.Write("offset1 is later than offset2 "); } else if (value < 0) { Console.Write("offset1 is earlier than offset2"); } else { Console.Write("offset1 is equal to offset2"); } } } |
offset1 is equal to offset2
Reference:
Recommended Posts:
- Difference between Method Overriding and Method Hiding in C#
- C# | Random.Next() Method
- C# | Method Parameters
- C# | Math.Abs() Method | Set - 2
- C# | Method Overriding
- Main Method in C#
- Decimal.Add() Method in C#
- Method Hiding in C#
- C# | IsNullOrEmpty() Method
- Stack.Contains() Method in C#
- Stack.Pop() Method in C#
- C# | Uri.ToString() Method
- C# | Math.Exp() Method
- C# | Math.Abs() Method | Set - 1
- Queue.Contains() Method in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



