C#中的DateTimeOffset.EqualsExact()方法
C#中的DateTimeOffset.EqualsExact()方法用于确定当前DateTimeOffset对象是否与指定的DateTimeOffset对象表示相同的时间并具有相同的偏移量。
语法
以下是语法-
public bool EqualsExact (DateTimeOffset val);
上面,Val是要比较的对象。
示例
现在让我们看一个实现DateTimeOffset.EqualsExact()方法的示例-
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"); } } }
输出结果
这将产生以下输出-
DateTimeOffset1 = 11/9/2019 5:30:10 AM -05:00 DateTimeOffset2 = 11/9/2019 5:30:10 AM -05:00 DateTimeOffset1 = DateTimeOffset1
示例
现在让我们来看另一个实现DateTimeOffset.EqualsExact()方法的示例-
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"); } } }
输出结果
这将产生以下输出-
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