DateTime.ToBinary() Method in C#


The DateTime.ToBinary() method in C# is used to serialize the current DateTime object to a 64-bit binary value that can be used to recreate the DateTime object. The return value is a 64-bit signed integer.

Syntax

Following is the syntax −

public long ToBinary ();

Example

Let us now see an example to implement the DateTime.ToBinary() method −

using System;
public class Demo {
   public static void Main() {
      DateTime d = new DateTime(2019, 10, 10, 8, 10, 40);
      Console.WriteLine("Date = {0}", d);
      long res = d.ToBinary();
      Console.WriteLine("64-bit binary value : {0}", res);
   }
}

Output

This will produce the following output −

Date = 10/10/2019 8:10:40 AM
64-bit binary value : 637062918400000000

Example

Let us now see another example to implement the DateTime.ToBinary() method −

using System;
public class Demo {
   public static void Main() {
      DateTime d = DateTime.Now;
      Console.WriteLine("Date = {0}", d);
      long res = d.ToBinary();
      Console.WriteLine("64-bit binary value : {0}", res);
   }
}

Output

This will produce the following output −

Date = 10/16/2019 8:15:56 AM
64-bit binary value : -8586303931293472296

Updated on: 07-Nov-2019

589 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements