The DateTimeOffset.EqualsExact() method in C# is used to determine whether the current DateTimeOffset object represents the same time and has the same offset as a specified DateTimeOffset object.
Syntax
Following is the syntax −
public bool EqualsExact (DateTimeOffset val);
Above, Val is the object to compare.
Example
Let us now see an example to implement the DateTimeOffset.EqualsExact() method −
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset1 = new DateTimeOffset(2019, 11, 09, 5, 30, 10, new TimeSpan(-5, 0, 0));
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(2019, 11,09, 5, 30, 10, new TimeSpan(-5, 0, 0));
Console.WriteLine("DateTimeOffset1 = {0}", dateTimeOffset1);
Console.WriteLine("DateTimeOffset2 = {0}", dateTimeOffset2);
bool res = dateTimeOffset1.EqualsExact(dateTimeOffset2);
if (res){
Console.Write("DateTimeOffset1 = DateTimeOffset2 ");
} else {
Console.Write("DateTimeOffset1 and DateTimeOffset2 are different");
}
}
}Output
This will produce the following output −
DateTimeOffset1 = 11/9/2019 5:30:10 AM -05:00 DateTimeOffset2 = 11/9/2019 5:30:10 AM -05:00 DateTimeOffset1 = DateTimeOffset1
Example
Let us now see another example to implement the DateTimeOffset.EqualsExact() method −
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset1 = new DateTimeOffset(2019, 09, 09, 5, 30, 10, new TimeSpan(-5, 0, 0));
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(2019, 11, 09, 9, 20, 20, new TimeSpan(-5, 0, 0));
Console.WriteLine("DateTimeOffset1 = {0}", dateTimeOffset1);
Console.WriteLine("DateTimeOffset2 = {0}", dateTimeOffset2);
bool res = dateTimeOffset1.EqualsExact(dateTimeOffset2);
if (res) {
Console.Write("DateTimeOffset1 = DateTimeOffset2 ");
} else {
Console.Write("DateTimeOffset1 and DateTimeOffset2 are different");
}
}
}Output
This will produce the following output −
DateTimeOffset1 = 9/9/2019 5:30:10 AM -05:00 DateTimeOffset2 = 11/9/2019 9:20:20 AM -05:00 DateTimeOffset1 and DateTimeOffset2 are different