当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。