DateTimeOffset.ToUnixTimeSeconds 方法

定義

傳回自 1970-01-01T00:00:00Z 以來所經過的秒數。

public:
 long ToUnixTimeSeconds();
public long ToUnixTimeSeconds ();
member this.ToUnixTimeSeconds : unit -> int64
Public Function ToUnixTimeSeconds () As Long

傳回

自 1970-01-01T00:00:00Z 以來所經過的秒數。

範例

下列範例會 ToUnixTimeSeconds 呼叫 方法,以傳回值在 1970-01-01-01T00:00:00Z 之後相等且不久之後的值 Unix 時間。

using System;

public class Example
{
   public static void Main()
   {
      DateTimeOffset dto = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
      Console.WriteLine("{0} --> Unix Seconds: {1}", dto, dto.ToUnixTimeSeconds());

      dto = new DateTimeOffset(1969, 12, 31, 23, 59, 0, TimeSpan.Zero);
      Console.WriteLine("{0} --> Unix Seconds: {1}", dto, dto.ToUnixTimeSeconds());

      dto = new DateTimeOffset(1970, 1, 1, 0, 1, 0, TimeSpan.Zero);
      Console.WriteLine("{0} --> Unix Seconds: {1}", dto, dto.ToUnixTimeSeconds());
   }
}
// The example displays the following output:
//    1/1/1970 12:00:00 AM +00:00 --> Unix Seconds: 0
//    12/31/1969 11:59:00 PM +00:00 --> Unix Seconds: -60
//    1/1/1970 12:01:00 AM +00:00 --> Unix Seconds: 60
open System

[<EntryPoint>]
let main _ =
    let dto = DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero)
    printfn $"{dto} --> Unix Seconds: {dto.ToUnixTimeSeconds()}"

    let dto = DateTimeOffset(1969, 12, 31, 23, 59, 0, TimeSpan.Zero)
    printfn $"{dto} --> Unix Seconds: {dto.ToUnixTimeSeconds()}"

    let dto = DateTimeOffset(1970, 1, 1, 0, 1, 0, TimeSpan.Zero)
    printfn $"{dto} --> Unix Seconds: {dto.ToUnixTimeSeconds()}"

    0
// The example displays the following output:
//    1/1/1970 12:00:00 AM +00:00 --> Unix Seconds: 0
//    12/31/1969 11:59:00 PM +00:00 --> Unix Seconds: -60
//    1/1/1970 12:01:00 AM +00:00 --> Unix Seconds: 60
Module Example
   Public Sub Main()
      Dim dto As DateTimeOffset = New DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero)
      Console.WriteLine("{0} --> Unix Seconds: {1}", dto, dto.ToUnixTimeSeconds())

      dto = New DateTimeOffset(1969, 12, 31, 23, 59, 0, TimeSpan.Zero)
      Console.WriteLine("{0} --> Unix Seconds: {1}", dto, dto.ToUnixTimeSeconds())

      dto = New DateTimeOffset(1970, 1, 1, 0, 1, 0, TimeSpan.Zero)
      Console.WriteLine("{0} --> Unix Seconds: {1}", dto, dto.ToUnixTimeSeconds())
   End Sub
End Module
' The example displays the following output:
'       1/1/1970 12:00:00 AM +00:00 --> Unix Seconds: 0
'       12/31/1969 11:59:00 PM +00:00 --> Unix Seconds: -60
'       1/1/1970 12:01:00 AM +00:00 --> Unix Seconds: 60

備註

Unix 時間代表自 1970-01-01T00:00:00Z (1970 年 1 月 1 日上午 12:00 UTC) 起經過的秒數。 它不需要將閏秒納入考慮。

這個方法會先將目前的實例轉換為 UTC,然後再傳回其 Unix 時間。 對於 1970-01-01T00:00:00Z 之前的日期和時間值,此方法會傳回負值。

適用於

另請參閱