📜  C#中的DateTimeOffset.EqualsExact()方法

📅  最后修改于: 2021-05-29 16:46:09             🧑  作者: Mango

DateTimeOffset.EqualsExact(DateTimeOffset)方法用于确定当前的DateTimeOffset对象是否与指定的DateTimeOffset对象表示相同的时间并具有相同的偏移量。

下面的程序说明了DateTimeOffset.EqualsExact(DateTimeOffset)方法的用法:

范例1:

// C# program to demonstrate the
// DateTimeOffset.EqualsExact(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 EqualsExact() method
        bool value = offset1.EqualsExact(offset2);
  
        if (value) 
        {
            Console.Write("offset1 is same as offset2 ");
        }
        else 
        {
            Console.Write("offset1 is not same as offset2");
        }
    }
}
输出:
offset1 is not same as offset2

范例2:

// C# program to demonstrate the
// DateTimeOffset.EqualsExact(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 EqualsExact() method
        bool value = offset1.EqualsExact(offset2);
  
        if (value) 
        {
            Console.Write("offset1 is same as offset2 ");
        }
        else 
        {
            Console.Write("offset1 is not same as offset2");
        }
    }
}
输出:
offset1 is same as offset2

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.datetimeoffset.equalsexact?view=netframework-4.7.2