Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
DateTime.FromBinary() Method in C#
The DateTime.FromBinary() method in C# is used to deserialize a 64-bit binary value and recreate an original serialized DateTime object. This method is particularly useful when you need to restore a DateTime object that was previously converted to its binary representation using ToBinary().
Syntax
Following is the syntax −
public static DateTime FromBinary(long dateData);
Parameters
dateData: A 64-bit signed integer that encodes the Kind property in a 2-bit field and the Ticks property in a 62-bit field.
Return Value
This method returns a DateTime object that is equivalent to the DateTime object that was serialized by the ToBinary() method.
Using FromBinary() with ToBinary()
The most common use case is to reconstruct a DateTime object from its binary representation −
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2019, 11, 10, 6, 20, 45);
long val = d1.ToBinary();
DateTime d2 = DateTime.FromBinary(val);
Console.WriteLine("Original DateTime = {0:yyyy-MM-dd HH:mm:ss}", d1);
Console.WriteLine("Binary value = {0}", val);
Console.WriteLine("Restored DateTime = {0:yyyy-MM-dd HH:mm:ss}", d2);
Console.WriteLine("Are they equal? {0}", d1.Equals(d2));
}
}
The output of the above code is −
Original DateTime = 2019-11-10 06:20:45 Binary value = 636828120450000000 Restored DateTime = 2019-11-10 06:20:45 Are they equal? True
Using FromBinary() with Different DateTimeKind
The method preserves the DateTimeKind property when deserializing −
using System;
public class Demo {
public static void Main() {
DateTime utcTime = DateTime.UtcNow;
DateTime localTime = DateTime.Now;
long utcBinary = utcTime.ToBinary();
long localBinary = localTime.ToBinary();
DateTime restoredUtc = DateTime.FromBinary(utcBinary);
DateTime restoredLocal = DateTime.FromBinary(localBinary);
Console.WriteLine("UTC Time Kind: {0}", restoredUtc.Kind);
Console.WriteLine("Local Time Kind: {0}", restoredLocal.Kind);
}
}
The output of the above code is −
UTC Time Kind: Utc Local Time Kind: Local
Using FromBinary() with Custom Binary Value
You can also create a DateTime from a specific binary value −
using System;
public class Demo {
public static void Main() {
DateTime d1 = DateTime.FromBinary(100000);
Console.WriteLine("DateTime from binary 100000: {0:yyyy-MM-dd HH:mm:ss.fff}", d1);
Console.WriteLine("Kind: {0}", d1.Kind);
Console.WriteLine("Ticks: {0}", d1.Ticks);
}
}
The output of the above code is −
DateTime from binary 100000: 0001-01-01 00:00:00.010 Kind: Unspecified Ticks: 100000
Conclusion
The DateTime.FromBinary() method is essential for deserializing DateTime objects from their binary representation. It preserves both the time value and the DateTimeKind property, making it perfect for data storage and transmission scenarios where DateTime objects need to be converted to and from binary format.
