DateTimeOffset.ToUnixTimeSeconds方法用于返回自1970-01-01T00:00:00Z以来经过的秒数。在返回Unix时间之前,此方法会将当前实例转换为UTC。而且,它将为1970-01-01T00:00:00Z之前的日期和时间值返回负值。
Syntax: public long ToUnixTimeSeconds ();
Return Value: This method return the number of seconds that have elapsed since 1970-01-01T00:00:00Z.
下面的程序说明了DateTimeOffset.ToUnixTimeSeconds()方法的用法:
范例1:
// C# program to demonstrate the
// DateTimeOffset.ToUnixTimeMilliseconds()
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// creating object of DateTimeOffset
DateTimeOffset offset = new DateTimeOffset(2017,
6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0));
// Returns the number of seconds
// that have elapsed since 1970-01-01T00:00:00Z.
// instance using ToUnixTimeSeconds() method
long value = offset.ToUnixTimeSeconds();
// Display the time
Console.WriteLine("Returns the number of"+
" seconds : {0}", value);
}
}
输出:
Returns the number of seconds : 1496321700
范例2:
// C# program to demonstrate the
// DateTimeOffset.ToUnixTimeSeconds()
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// creating object of DateTimeOffset
DateTimeOffset offset = new DateTimeOffset(2017,
6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0));
// Returns the number of seconds
// that have elapsed since 1970-01-01T00:00:00Z.
// instance using ToUnixTimeSeconds() method
long value = offset.ToUnixTimeSeconds();
// Display the time
Console.WriteLine("Returns the number of"+
" seconds : {0}", value);
}
}
输出:
Returns the number of seconds : 1496321700
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.datetimeoffset.tounixtimeseconds?view=netframework-4.7.2