DateTime.ToBinary Method


将当前 DateTime 对象序列化为一个 64 位二进制值,该值随后可用于重新创建 DateTime 对象。Serializes the current DateTime object to a 64-bit binary value that subsequently can be used to recreate the DateTime object.

 long ToBinary();
public long ToBinary ();
member this.ToBinary : unit -> int64
Public Function ToBinary () As Long



64 位有符号整数,它对 KindTicks 属性进行了编码。A 64-bit signed integer that encodes the Kind and Ticks properties.


使用 ToBinary 方法可以将当前 DateTime 对象的值转换为二进制值。Use the ToBinary method to convert the value of the current DateTime object to a binary value. 随后,使用二进制值和 FromBinary 方法来重新创建原始 DateTime 对象。Subsequently, use the binary value and the FromBinary method to recreate the original DateTime object.


在某些情况下,FromBinary 方法返回的 DateTime 值与提供给 ToBinary 方法的原始 DateTime 值不同。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

本地时间(即调整为本地时区的协调世界时)由其 Kind 属性具有值 LocalDateTime 结构来表示。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. ToBinary 方法生成的二进制表示形式还原本地 DateTime 值时,FromBinary 方法可能会调整重新创建的值,使其与原始值不相等。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:

  • 如果本地 DateTime 对象按 ToBinary 方法在一个时区中序列化,然后 FromBinary 方法在不同时区中进行反序列化,则生成的 DateTime 对象表示的本地时间会自动调整为第二个时区。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.

    例如,假设一个表示本地时间的 DateTime 对象。For example, consider a DateTime object that represents a local time of 3 P.M. 在美国西部区域中执行的应用程序使用 ToBinary 方法将该 DateTime 对象转换为二进制值。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. 在美国东部时区正在执行的另一个应用程序使用 FromBinary 方法将二进制值转换为新的 DateTime 对象。Another application that is executing in the U.S. Eastern Time zone uses the FromBinary method to convert the binary value to a new DateTime object. DateTime 对象的值为下午6点,表示与原来的下午3点相同的时间点。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.

  • 如果本地 DateTime 值的二进制表示形式表示在其上调用了 FromBinary 的系统的本地时区中的无效时间,则将调整该时间,使其有效。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.

    例如,从标准时间转换到夏令时的时间是在2010年3月14日上午2:00,上午,时间前进一小时,到凌晨3:00For example, the transition from standard time to daylight saving time occurs in the U.S. Pacific Time zone 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. 下面的示例显示,在此范围内的时间由 ToBinary 方法转换为二进制值,然后由 FromBinary 方法还原时,原始值将调整为有效时间。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. 如示例所示,您可以通过将特定日期和时间值传递到 TimeZoneInfo.IsInvalidTime 方法来确定是否可以修改该日期和时间值。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.",
          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
    Module Example
       Public Sub Main()
          Dim localDate As Date = DateTime.SpecifyKind(#03/14/2010 2:30AM#, DateTimeKind.Local)
          Dim binLocal As Long = localDate.ToBinary()
          If TimeZoneInfo.Local.IsInvalidTime(localDate) Then
             Console.WriteLine("{0} is an invalid time in the {1} zone.", _
                               localDate, _
          End If
          Dim localDate2 As Date = DateTime.FromBinary(binLocal)
          Console.WriteLine("{0} = {1}: {2}", _
                            localDate, localDate2, localDate.Equals(localDate2))
       End Sub
    End Module
    ' 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

从 .NET Framework 版本2.0 开始,DateTime 结构包含一个私有类型字段,该字段指示指定的时间值是基于本地时间、协调世界时(UTC)还是两者都不是,后者包含指定日期和时间的100毫微秒刻度数。Starting with the .NET Framework version 2.0, a DateTime structure consists of a private Kind field, which indicates whether the specified time value is based on local time, Coordinated Universal Time (UTC), or neither, and a private Ticks field, which contains the number of 100-nanosecond ticks that specify a date and time. 可以通过 Ticks 属性访问刻度字段,并且可以通过 Kind 属性访问 Kind 字段。The Ticks field can be accessed with the Ticks property and the Kind field can be accessed with the Kind property.

在 .NET Framework 2.0 之前,如果手动序列化 DateTime 对象,而不是使用序列化接口(如 System.Runtime.Serialization.ISerializable),则只需要序列化 DateTime 结构中的滴答数据。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 only needed to serialize the Ticks data in the DateTime structure. 从版本2.0 开始,还必须序列化类型数据。Starting with version 2.0, you must also serialize the Kind data.

Applies to

See also