DateTimeOffset.FromUnixTimeSeconds(Int64)方法用于将表示从1970-01-01T00:00:00Z开始经过的秒数的Unix时间转换为DateTimeOffset值。
用法: public static DateTimeOffset FromUnixTimeSeconds (long seconds);
Here, it takes a Unix time, expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at 12:00 AM UTC). For Unix times before this date, its value is negative.
返回值:此方法返回的日期和时间值表示与Unix时间相同的时刻。
异常:如果秒数小于-62,135,596,800或秒数大于253,402,300,799,则此方法将提供ArgumentOutOfRangeException。
以下示例程序旨在说明DateTimeOffset.FromUnixTimeSeconds(Int64)方法的用法:
示例1:
// C# program to demonstrate the
// DateTimeOffset.FromUnixTimeSeconds(Int64)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// Converts a Unix time expressed
// as the number of seconds
// that have elapsed since
// 1970-01-01T00:00:00Z to a
// DateTimeOffset value instance
// using FromUnixTimeSeconds() method
DateTimeOffset value =
DateTimeOffset.FromUnixTimeSeconds(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/1970 02:46:40 +00:00
示例2:对于ArgumentOutOfRangeException
// C# program to demonstrate the
// DateTimeOffset.FromUnixTimeSeconds(Int64)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// Converts a Unix time expressed
// as the number of seconds
// that have elapsed since
// 1970-01-01T00:00:00Z to a
// DateTimeOffset value instance
// using FromUnixTimeSeconds() method
DateTimeOffset value =
DateTimeOffset.FromUnixTimeSeconds(254402300799);
// 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
参考:
相关用法
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 DateTimeOffset.FromUnixTimeSeconds() Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。