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


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

DateTimeOffset.ToFileTime方法用於將當前DateTimeOffset對象的值轉換為Windows文件時間。

用法: public long ToFileTime ();

返回值:此方法返回當前DateTimeOffset對象的值,表示為Windows文件時間。


異常:如果生成的文件時間代表了1601年1月1日美國協調世界時(UTC)午夜之前的日期和時間,則此方法將提供ArgumentOutOfRangeException。

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

示例1:

// C# program to demonstrate the 
// DateTimeOffset.ToFileTime() 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // creating object of  DateTimeOffset 
            DateTimeOffset offset = new DateTimeOffset(2007, 
                    6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0)); 
  
            // Converts the value of the current 
            // DateTimeOffset object to a Windows file time. 
            // instance using ToFileTime() method 
            long value = offset.ToFileTime(); 
  
            // Display the time 
            Console.WriteLine("Windows file time is {0}", value); 
        } 
  
        catch (ArgumentOutOfRangeException e)  
        { 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
Windows file time is 128251761000000000

示例2:對於ArgumentOutOfRangeException

// C# program to demonstrate the 
// DateTimeOffset.ToFileTime() 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // creating object of  DateTimeOffset 
            DateTimeOffset offset = new DateTimeOffset(1600, 
                    6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0)); 
  
            // Converts the value of the current 
            // DateTimeOffset object to a Windows file time. 
            // instance using ToFileTime() method 
            long value = offset.ToFileTime(); 
  
            // Display the time 
            Console.WriteLine("Windows file time is {0}", value); 
        } 
  
        catch (ArgumentOutOfRangeException e)  
        { 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
Exception Thrown: System.ArgumentOutOfRangeException

參考:



相關用法


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