C# DateTime FromBinary() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DateTime

Definition

In C#, DateTime.FromBinary takes a 64-bit number (long) and returns the DateTime it stands for. The number must come from ToBinary().

The binary form packs two things into one number: the ticks and the Kind. That makes it a compact way to save a date and get the same Kind back later.

This is a static member of System.DateTime. You call it with the type name, such as DateTime.FromBinary(...). DateTime holds a date and time of day with a Kind of Utc, Local, or Unspecified.

Syntax

Call it with the type name and a long from ToBinary.

C#
DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
long bin = dt.ToBinary();
DateTime back = DateTime.FromBinary(bin);   // same ticks and Kind
CallWhat it does
DateTime.FromBinary(dt.ToBinary())Round trip: you get the same value back.
DateTime.FromBinary(0)Zero is DateTime.MinValue.
DateTime.FromBinary(long.Parse(text))Restore a value stored as text.

A random number is not a valid input. Values that do not describe a date in range throw ArgumentException. For a Local value, the stored number holds UTC ticks and the result is converted to the local zone of the computer that reads it.

Library declarations
C#
public static DateTime FromBinary(long dateData);

Parameters

ParameterDescription
dateDataRequired. A 64-bit number created by DateTime.ToBinary().

Return Value

Return ValueDescription
DateTimeThe restored DateTime, with the same Kind that was stored.

C# Examples

Example 1 — Unspecified Round Trip

Save an Unspecified value as a long and restore it.

C#
using System;

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

How It Works

  1. ToBinary first. The number 639306090450000000 is the tick count for this moment.
  2. Unspecified is plain ticks. With Kind Unspecified, the binary form equals Ticks.
  3. Back to DateTime. FromBinary gives a value equal to the original, so the check prints True.
  4. Kind kept. The restored value is still Unspecified.

Example 2 — Utc Round Trip

Show that the binary form of a Utc value is larger and that Kind is restored.

C#
using System;

class Program {
    static void Main() {
        DateTime utc = new DateTime(2026, 11, 18, 14, 30, 45, DateTimeKind.Utc);
        long bin = utc.ToBinary();
        DateTime back = DateTime.FromBinary(bin);
        Console.WriteLine(bin);
        Console.WriteLine(back.Kind);
        Console.WriteLine(back == utc);
    }
}

How It Works

  1. Bigger number. 5250992108877387904 is the ticks plus 4611686018427387904, a bit set to mean Utc.
  2. Kind stored. The two top bits of the number say Unspecified, Utc, or Local.
  3. Restored Kind. back.Kind prints Utc, which a plain tick count could not keep.
  4. Equal value. The ticks match too, so == prints True.

Example 3 — Store the Number as Text

Write the number to text, read it back, and restore the date. Also show zero.

C#
using System;
using System.Globalization;

class Program {
    static string Show(DateTime d) => d.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

    static void Main() {
        DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
        string saved = dt.ToBinary().ToString(CultureInfo.InvariantCulture);
        long number = long.Parse(saved, CultureInfo.InvariantCulture);
        Console.WriteLine(Show(DateTime.FromBinary(number)));
        Console.WriteLine(Show(DateTime.FromBinary(0)));
    }
}

How It Works

  1. Number to text. The long is written with the invariant culture so there are no group separators.
  2. Text to number. long.Parse returns the same number.
  3. Date restored. The first line shows the original date and time.
  4. Zero. A binary value of 0 means zero ticks, which is January 1 of year 1 (DateTime.MinValue).

Frequently Asked Questions

It rebuilds a DateTime from a long created by ToBinary, including the Kind.
Only for Unspecified values, where the binary form equals the tick count. Other Kinds use extra bits.
It is compact and keeps Kind, with no culture or format problems.
FromBinary throws ArgumentException when the data does not describe a valid DateTime.
It is DateTime.MinValue, January 1, year 1, 00:00:00.

Next: FromFileTime()

Convert a Windows file time number to a DateTime in local time with System.DateTime.

FromFileTime() 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.

11 people found this page helpful