Condividi tramite


DateTime.FromBinary Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Shared Function FromBinary ( _
    dateData As Long _
) As DateTime
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 original DateTime object.

Exceptions

Exception Condition
ArgumentException

dateData is less than DateTime.MinValue or greater than DateTime.MaxValue.

Remarks

Use the FromBinary method to restore the original value of a DateTime value that was converted to an Int64 value by the DateTime.ToBinary method.

Important noteImportant Note:

   In some cases, the DateTime value returned by the FromBinary method is not identical to the original DateTime value supplied to the DateTime.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 value whose Kind property has the value Local. When restoring a local DateTime value from its Int64 equivalent, 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 DateTime.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. A Web server application that is executing in the U.S. Pacific Time zone uses the DateTime.ToBinary method to convert that date and time to a binary value. A Silverlight-based 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 Standard 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. When a time that falls within this range is converted to a binary value by the DateTime.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.

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.