Serialize DateTime to a 64-bit binary value in CSharp

Description

The following code shows how to serialize DateTime to a 64-bit binary value.

Example


/*w w  w . jav a  2  s.c o m*/
using System;

public class Example
{
   public static void Main()
   {
      DateTime localDate = new DateTime(2014, 3, 14, 2, 30, 0, DateTimeKind.Local);
      long binLocal = localDate.ToBinary();
      
      if (TimeZoneInfo.Local.IsInvalidTime(localDate))
         Console.WriteLine("{0} is an invalid time in the {1} zone.", 
                           localDate, 
                           TimeZoneInfo.Local.StandardName);

      DateTime localDate2 = DateTime.FromBinary(binLocal);
      Console.WriteLine("{0} = {1}: {2}", 
                        localDate, localDate2, localDate.Equals(localDate2));
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var