DateTime.FromBinary() Method in C#


The DateTime.FromBinary() method in C# is used to deserialize a 64-bit binary value and recreates an original serialized DateTime object.

Syntax

Following is the syntax −

public static DateTime FromBinary (long val);

Above, Val is a 64-bit signed integer that encodes the Kind property in a 2-bit field and the Ticks property in a 62-bit field.

Example

Let us now see an example to implement the DateTime.FromBinary() method −

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);
      System.Console.WriteLine("Initial DateTime = {0:y} {0:dd} ",d1);
      System.Console.WriteLine("
New DateTime = {0:y} {0:dd} ", d2);    } }

Output

This will produce the following output −

Initial DateTime = November 2019 10
New DateTime = November 2019 10

Example

Let us now see another example to implement the DateTime.FromBinary() method −

using System;
public class Demo {
   public static void Main() {
      DateTime d1 = DateTime.FromBinary(100000);
      System.Console.WriteLine("DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}",d1);
   }
}

Output

This will produce the following output −

DateTime = 01 January 0001, 12:00:00

Updated on: 07-Nov-2019

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements