C# | DateTime.ToBinary() Method with Example

DateTime.ToBinary() Method: Here, we are going to learn about the ToBinary() method of DateTime class with example in C#. By IncludeHelp Last updated : August 27, 2023

C# DateTime.ToBinary() Method

DateTime.ToBinary() method is used to serialize the object of the DateTime class into 64-bit binary value.


long DateTime.ToBinary();


  • It does not accept any parameter.

Return value

The return type of this method is long, it returns a binary value representing the DateTime object.

Example to demonstrate example of DateTime.ToBinary() method

using System;

namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            //Create object of datetime class
            DateTime dt1 = new DateTime(2019,1,1,5,0,15);

            //Serializes the dt1 to 64-bit binary value
            long binVal = dt1.ToBinary();

            //Print the value
            Console.WriteLine("64-bit binary value of dt1 : " + binVal);



64-bit binary value of dt1 : 636819156150000000

In the above program, we took the dt1 object of DateTime class and serialize dt1 into 64-bit value using ToBinary() method and holds the result into binVal variable and print it on the console screen.

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.