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-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 時 UTC) 以降に経過した秒数を表します。 うるう秒は考慮されません。

このメソッドは、Unix 時刻を返す前に、最初に現在のインスタンスを UTC に変換します。 1970-01-01T00:00:00Z より前の日付と時刻の値の場合、このメソッドは負の値を返します。

適用対象

こちらもご覧ください