DateTimeOffset.ToUnixTimeSeconds Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mengembalikan jumlah detik yang telah berlalu sejak 1970-01-01T00:00:00Z.
public:
long ToUnixTimeSeconds();
public long ToUnixTimeSeconds();
member this.ToUnixTimeSeconds : unit -> int64
Public Function ToUnixTimeSeconds () As Long
Mengembalikan
Jumlah detik yang telah berlalu sejak 1970-01-01T00:00:00Z.
Contoh
Contoh berikut memanggil ToUnixTimeSeconds metode untuk mengembalikan waktu Unix nilai yang sama dengan, tak lama sebelumnya, dan tak lama setelah 1970-01-01T00:00:00Z.
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
Keterangan
Waktu Unix menunjukkan jumlah detik yang telah berlalu sejak 1970-01-01T00:00:00Z (1 Januari 1970, pukul 12.00 UTC). Tidak perlu waktu beberapa detik untuk memperhitungkan lompatan.
Metode ini terlebih dahulu mengonversi instans saat ini ke UTC sebelum mengembalikan waktu Unix-nya. Untuk nilai tanggal dan waktu sebelum 1970-01-01T00:00:00Z, metode ini mengembalikan nilai negatif.