DateTime.FromBinary Method

Deserializes a 64-bit binary value and recreates an original serialized DateTime object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static DateTime FromBinary(
	long dateData
)

Parameters

dateData
Type: System.Int64

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

Type: System.DateTime
An object that is equivalent to the DateTime object that was serialized by the ToBinary method.

ExceptionCondition
ArgumentException

dateData is less than MinValue or greater than MaxValue.

Use the ToBinary method to convert the value of the current DateTime object to a binary value. Subsequently, use the binary value and the FromBinary method to recreate the original DateTime object.

Important noteImportant

In some cases, the DateTime value returned by the FromBinary method is not identical to the original DateTime value supplied to the ToBinary method. For more information, see the next section, "Local Time Adjustment".

Local Time Adjustment

A local time, which is a Coordinated Universal Time adjusted to the local time zone, is represented by a DateTime structure whose Kind property has the value Local. When restoring a local DateTime value from the binary representation that is produced by the ToBinary method, the FromBinary method may adjust the recreated value so that it is not equal to the original value. This can occur under the following conditions:

  • If a local DateTime object is serialized in one time zone by the ToBinary method, and then deserialized in a different time zone by the FromBinary method, the local time represented by the resulting DateTime object is automatically adjusted to the second time zone.

    For example, consider a DateTime object that represents a local time of 3 P.M. An application that is executing in the U.S. Pacific Time zone uses the ToBinary method to convert that DateTime object to a binary value. Another application that is executing in the U.S. Eastern Time zone then uses the FromBinary method to convert the binary value to a new DateTime object. The value of the new DateTime object is 6 P.M., which represents the same point in time as the original 3 P.M. value, but is adjusted to local time in the Eastern Time zone.

  • If the binary representation of a local DateTime value represents an invalid time in the local time zone of the system on which FromBinary is called, the time is adjusted so that it is valid.

    For example, the transition from standard time to daylight saving time occurs in the Pacific Time zone of the United States on March 14, 2010, at 2:00 A.M., when the time advances by one hour, to 3:00 A.M. This hour interval is an invalid time, that is, a time interval that does not exist in this time zone. The following example shows that when a time that falls within this range is converted to a binary value by the ToBinary method and is then restored by the FromBinary method, the original value is adjusted to become a valid time. You can determine whether a particular date and time value may be subject to modification by passing it to the TimeZoneInfo.IsInvalidTime method, as the example illustrates.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          DateTime localDate = new DateTime(2010, 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 example displays the following output: 
    //    3/14/2010 2:30:00 AM is an invalid time in the Pacific Standard Time zone. 
    //    3/14/2010 2:30:00 AM = 3/14/2010 3:30:00 AM: False
    

Version Considerations

Starting with the .NET Framework version 2.0, a DateTime structure consists of a private Kind field, which indicates whether the specified time is local time, Coordinated Universal Time (UTC), or neither, concatenated to a private Ticks field, which contains the number of 100-nanosecond ticks that specify a date and time. The number of ticks can be accessed with the Ticks property and the Kind field can be accessed with the Kind property.

Prior to the .NET Framework 2.0, if you serialized a DateTime object manually instead of using a serialization interface such as System.Runtime.Serialization.ISerializable, you needed to serialize only the Ticks data in the DateTime. Starting with the .NET Framework 2.0, you must also serialize the Kind data.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft