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

📅  最后修改于: 2021-05-29 14:38:27             🧑  作者: Mango

DateTimeOffset.FromFileTime(Int64)方法用于将指定的Windows文件时间转换为等效的本地时间。

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

范例1:

// C# program to demonstrate the
// DateTimeOffset.FromFileTime(Int64)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // converts the specified Windows file time
            // to an equivalent local time.
            // instance using FromFileTime() method
            DateTimeOffset value = DateTimeOffset.FromFileTime(10000);
  
            // Display the time
            Console.WriteLine("DateTimeOffset is {0}", value);
        }
  
        catch (ArgumentOutOfRangeException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
DateTimeOffset is 01/01/1601 00:00:00 +00:00

示例2:对于ArgumentOutOfRangeException

// C# program to demonstrate the
// DateTimeOffset.FromFileTime(Int64)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // converts the specified Windows file time
            // to an equivalent local time.
            // instance using FromFileTime() method
            DateTimeOffset value = DateTimeOffset.FromFileTime(-1);
  
            // Display the time
            Console.WriteLine("DateTimeOffset is {0}", value);
        }
  
        catch (ArgumentOutOfRangeException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
Exception Thrown: System.ArgumentOutOfRangeException

参考:

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