📜  C#中的DateTime.ToBinary()方法

📅  最后修改于: 2021-05-29 13:27:51             🧑  作者: Mango

此方法用于将当前DateTime对象序列化为64位二进制值,该值随后可用于重新创建DateTime对象。

下面的程序说明了DateTime.ToBinary()方法的用法

范例1:

// C# program to demonstrate the
// DateTime.ToBinary()
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // creating object of DateTime
        DateTime date = new DateTime(2011, 1,
                                1, 4, 0, 15);
  
        // Serializes the current DateTime 
        // object to a 64-bit binary value
        // using ToBinary() method;
        long value = date.ToBinary();
  
        // Display the value
        Console.WriteLine("64-bit binary value : {0}",
                                               value);
    }
}
输出:
64-bit binary value : 634294512150000000

范例2:

// C# program to demonstrate the
// DateTime.ToBinary()
// Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // calling check() method
        check(new DateTime(2010, 1,
                     3, 4, 0, 15));
  
        check(new DateTime(2010, 1,
                     5, 4, 0, 15));
    }
  
    public static void check(DateTime date)
    {
  
        // Serializes the current DateTime 
        // object to a 64-bit binary value
        // using ToBinary() method;
        long value = date.ToBinary();
  
        // Display the value
        Console.WriteLine("64-bit binary value"+
                 " of {0} is {1}", date, value);
    }
}
输出:
64-bit binary value of 01/03/2010 04:00:15 is 633980880150000000
64-bit binary value of 01/05/2010 04:00:15 is 633982608150000000

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.datetime.tobinary?view=netframework-4.7.2