C# DateTime ToBinary

Description

DateTime ToBinary serializes the current DateTime object to a 64-bit binary value that subsequently can be used to recreate the DateTime object.

Syntax

DateTime.ToBinary has the following syntax.


public long ToBinary()

Returns

DateTime.ToBinary method returns A 64-bit signed integer that encodes the Kind and Ticks properties.

Example

The following code determines whether a particular date and time value may be subject to modification by passing it to the TimeZoneInfo.IsInvalidTime method.


// w w w.  j  a  v  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 »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version