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
Call
What 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
Parameter
Description
None
ToBinary() takes no parameters.
Return Value
Return Value
Description
long
A 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);
}
}
Output
639306090450000000
True
How It Works
One number. The date and time collapse into 639306090450000000.
Ticks since year 1. That is how many 100-nanosecond steps have passed since January 1, year 1.
Unspecified case. The Kind bits are zero, so the binary form is exactly Ticks.
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);
}
}
Output
4611686018427387904
1
How It Works
Subtract the ticks. What is left is only the Kind flag.
4611686018427387904. This is 2 to the power 62, a single bit near the top of the number.
Shift and mask.>> 62 moves the Kind bits down and & 3 keeps just those two bits.
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());
}
}
Output
8
True
True
How It Works
long is 8 bytes.BitConverter.GetBytes returns an array of length 8.
Read it back.ToInt64 and FromBinary rebuild the original date.
Equal after round trip. The second line prints True.
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.