DateTimeOffset.ToUnixTimeSeconds方法用於返回自1970-01-01T00:00:00Z以來經過的秒數。在返回Unix時間之前,此方法會將當前實例轉換為UTC。而且,它將為1970-01-01T00:00:00Z之前的日期和時間值返回負值。
用法: public long ToUnixTimeSeconds ();
返回值:此方法返回自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
參考:
相關用法
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 DateTimeOffset.ToUnixTimeSeconds() Method in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。