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 that encodes both the date/time value and the DateTimeKind.

Syntax

Following is the syntax −

public long ToBinary();

Return Value

The method returns a long (64-bit signed integer) that represents the serialized DateTime. This binary value can later be used with DateTime.FromBinary() to reconstruct the original DateTime object.

How It Works

The binary representation encodes different information based on the DateTimeKind:

  • Local DateTime: The high bit is set to 1, and the value includes timezone offset information

  • UTC DateTime: The high bit is set to 0, representing the raw tick count

  • Unspecified DateTime: Treated similar to local time

DateTime.ToBinary() Process DateTime Object ToBinary() 64-bit Binary Value FromBinary() Restored DateTime Preserves both value and DateTimeKind

Example with Specific DateTime

using System;

public class Demo {
   public static void Main() {
      DateTime d = new DateTime(2019, 10, 10, 8, 10, 40, DateTimeKind.Utc);
      Console.WriteLine("Date = {0}", d);
      Console.WriteLine("Kind = {0}", d.Kind);
      long res = d.ToBinary();
      Console.WriteLine("64-bit binary value: {0}", res);
      
      // Reconstruct the DateTime
      DateTime restored = DateTime.FromBinary(res);
      Console.WriteLine("Restored Date = {0}", restored);
   }
}

The output of the above code is −

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

Example with Current DateTime

using System;

public class Demo {
   public static void Main() {
      DateTime d = DateTime.Now;
      Console.WriteLine("Date = {0}", d);
      Console.WriteLine("Kind = {0}", d.Kind);
      long res = d.ToBinary();
      Console.WriteLine("64-bit binary value: {0}", res);
      
      // Note: Negative values indicate Local time
      Console.WriteLine("Is Local Time: {0}", res < 0);
   }
}

The output of the above code is −

Date = 12/15/2024 2:30:45 PM
Kind = Local
64-bit binary value: -8585766606465380352
Is Local Time: True

Common Use Cases

  • Serialization: Converting DateTime objects to binary format for storage in databases or files

  • Network Communication: Transmitting DateTime values across different systems

  • Caching: Storing DateTime values in binary format for performance optimization

Conclusion

The DateTime.ToBinary() method provides a reliable way to serialize DateTime objects into 64-bit binary values while preserving both the time value and DateTimeKind information. This serialized format can be stored, transmitted, and later reconstructed using DateTime.FromBinary().

Updated on: 2026-03-17T07:04:35+05:30

767 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements