當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# DateTimeOffset.ToUnixTimeMilliseconds()用法及代碼示例


DateTimeOffset.ToUnixTimeMilliseconds方法用於返回自1970-01-01T00:00:00.000Z以來經過的毫秒數。對於1970-01-01T00:00:00Z之前的日期和時間值,此方法將返回負值。

用法: public long ToUnixTimeMilliseconds ();

返回值:此方法返回自1970-01-01T00:00:00.000Z以來經過的毫秒數。


以下示例程序旨在說明DateTimeOffset.ToUnixTimeMilliseconds()方法的使用:

範例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(2007, 
                6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0)); 
  
        // Returns the number of milliseconds 
        // instance using ToUnixTimeMilliseconds() method 
        long value = offset.ToUnixTimeMilliseconds(); 
  
        // Display the time 
        Console.WriteLine("Returns the number of"+ 
                    " milliseconds:{0}", value); 
    } 
}
輸出:
Returns the number of milliseconds:1180702500000

範例2:

// 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(1960, 
                6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0)); 
  
        // Returns the number of milliseconds 
        // instance using ToUnixTimeMilliseconds() method 
        long value = offset.ToUnixTimeMilliseconds(); 
  
        // Display the time 
        Console.WriteLine("Returns the number "+ 
                "of milliseconds:{0}", value); 
    } 
}
輸出:
Returns the number of milliseconds:-302439900000

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 DateTimeOffset.ToUnixTimeMilliseconds() Method in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。