DateTimeOffset.FromFileTime(Int64)方法用于将指定的Windows文件时间转换为等效的本地时间。
Syntax: public static DateTimeOffset FromFileTime (long fileTime);
Here, it takes a Windows file time, expressed in ticks.
Return Value: This method returns an object that represents the date and time of fileTime with the offset set to the local time offset.
Exception: This method will give ArgumentOutOfRangeException if filetime is less than zero or filetime is greater than DateTimeOffset.MaxValue.Ticks.
下面的程序说明了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