日期时间。C# 中的 ToBinary()方法

原文:https://www . geesforgeks . org/datetime-tobinary-method-in-c-sharp/

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

语法:public long ToBinary();

返回值:此方法返回一个 64 位有符号整数,用于编码“种类”和“刻度”属性。

以下程序说明了日期时间的使用。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);
    }
}

Output:

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);
    }
}

Output:

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

参考: