C# DateTime ToBinary() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DateTime

Definition

In C#, DateTime.ToBinary() returns a long that holds the ticks of a date and its Kind. Use DateTime.FromBinary to turn the number back into a date.

It is a compact format for saving a date in a file, a database column, or a network message, without worrying about culture or text formats.

This is an instance member of System.DateTime. You call it on a DateTime value, such as dt.ToBinary(...). DateTime is a struct, so every value is copied and never changed in place.

Syntax

Call it on a DateTime. It takes no arguments.

C#
DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
long bin = dt.ToBinary();                    // 639306090450000000
DateTime back = DateTime.FromBinary(bin);    // same date
CallWhat it does
dt.ToBinary()Ticks and Kind as one long.
BitConverter.GetBytes(dt.ToBinary())The number as 8 bytes.
DateTime.FromBinary(bin)Turn the number back into a date.

For an Unspecified value the result equals Ticks. A Utc value adds 4611686018427387904 (bit 62). A Local value is stored as UTC ticks plus a flag, so its number depends on the time zone.

Library declarations
C#
public long ToBinary();

Parameters

ParameterDescription
NoneToBinary() takes no parameters.

Return Value

Return ValueDescription
longA 64-bit number: the ticks in the lower 62 bits and the Kind in the top two bits.

C# Examples

Example 1 — Binary Equals Ticks

Show that an Unspecified value has the same binary number and tick count.

C#
using System;

class Program {
    static void Main() {
        DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
        Console.WriteLine(dt.ToBinary());
        Console.WriteLine(dt.ToBinary() == dt.Ticks);
    }
}

How It Works

  1. One number. The date and time collapse into 639306090450000000.
  2. Ticks since year 1. That is how many 100-nanosecond steps have passed since January 1, year 1.
  3. Unspecified case. The Kind bits are zero, so the binary form is exactly Ticks.
  4. Prints True. This only holds for Unspecified values.

Example 2 — The Utc Flag

Compare the binary number of a Utc value with its Ticks and read the Kind bits.

C#
using System;

class Program {
    static void Main() {
        DateTime utc = new DateTime(2026, 11, 18, 14, 30, 45, DateTimeKind.Utc);
        Console.WriteLine(utc.ToBinary() - utc.Ticks);
        Console.WriteLine((utc.ToBinary() >> 62) & 3);
    }
}

How It Works

  1. Subtract the ticks. What is left is only the Kind flag.
  2. 4611686018427387904. This is 2 to the power 62, a single bit near the top of the number.
  3. Shift and mask. >> 62 moves the Kind bits down and & 3 keeps just those two bits.
  4. Kind code 1. The value 1 means Utc; 0 means Unspecified and the Local flag is the top bit.

Example 3 — Eight Bytes

Store the number as bytes, restore it, and check ordering.

C#
using System;

class Program {
    static void Main() {
        DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
        byte[] bytes = BitConverter.GetBytes(dt.ToBinary());
        DateTime back = DateTime.FromBinary(BitConverter.ToInt64(bytes, 0));
        Console.WriteLine(bytes.Length);
        Console.WriteLine(back == dt);
        Console.WriteLine(new DateTime(2026, 1, 1).ToBinary() < new DateTime(2026, 2, 1).ToBinary());
    }
}

How It Works

  1. long is 8 bytes. BitConverter.GetBytes returns an array of length 8.
  2. Read it back. ToInt64 and FromBinary rebuild the original date.
  3. Equal after round trip. The second line prints True.
  4. Order is kept. For the same Kind, a later date has a larger number, so the last line is True.

Frequently Asked Questions

A long with the ticks in the lower bits and the Kind in the top two bits.
Only for Unspecified values. For Utc and Local the Kind bits make the number different.
Use DateTime.FromBinary(number).
Yes for your own files and databases. It is compact and culture-free. For human-readable or cross-language data use the "o" format.
Yes. Local values are stored as UTC ticks plus a flag, so the number depends on the computer time zone.

Next: ToFileTime()

Convert a DateTime (treated as local time) to a Windows file time number with System.DateTime.

ToFileTime() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful