DateTimeOffset.LocalDateTime 属性

定义

获取 DateTime 值,该值表示当前 DateTimeOffset 对象的本地日期和时间。Gets a DateTime value that represents the local date and time of the current DateTimeOffset object.

public:
 property DateTime LocalDateTime { DateTime get(); };
public DateTime LocalDateTime { get; }
member this.LocalDateTime : DateTime
Public ReadOnly Property LocalDateTime As DateTime

属性值

当前的 DateTimeOffset 对象的本地日期和时间。The local date and time of the current DateTimeOffset object.

示例

下面的示例演示了在美国太平洋标准时区,将 DateTimeOffset 值转换为本地时间。The following example illustrates several conversions of DateTimeOffset values to local times in the U.S. Pacific Standard Time zone. 请注意,最后三次都不明确;属性将所有这些属性映射到太平洋标准时区中的单个日期和时间。Note that the last three times are all ambiguous; the property maps all of them to a single date and time in the Pacific Standard Time zone.

 DateTimeOffset dto;

 // Current time
 dto = DateTimeOffset.Now;
 Console.WriteLine(dto.LocalDateTime);
 // UTC time
 dto = DateTimeOffset.UtcNow;
 Console.WriteLine(dto.LocalDateTime);

// Transition to DST in local time zone occurs on 3/11/2007 at 2:00 AM
 dto = new DateTimeOffset(2007, 3, 11, 3, 30, 0, new TimeSpan(-7, 0, 0));      
 Console.WriteLine(dto.LocalDateTime);
 dto = new DateTimeOffset(2007, 3, 11, 2, 30, 0, new TimeSpan(-7, 0, 0));
 Console.WriteLine(dto.LocalDateTime);
 // Invalid time in local time zone
 dto = new DateTimeOffset(2007, 3, 11, 2, 30, 0, new TimeSpan(-8, 0, 0));
 Console.WriteLine(TimeZoneInfo.Local.IsInvalidTime(dto.DateTime));
 Console.WriteLine(dto.LocalDateTime);

 // Transition from DST in local time zone occurs on 11/4/07 at 2:00 AM
 // This is an ambiguous time
 dto = new DateTimeOffset(2007, 11, 4, 1, 30, 0, new TimeSpan(-7, 0, 0));
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto));
 Console.WriteLine(dto.LocalDateTime);
 dto = new DateTimeOffset(2007, 11, 4, 2, 30, 0, new TimeSpan(-7, 0, 0));           
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto));
 Console.WriteLine(dto.LocalDateTime);
 // This is also an ambiguous time
 dto = new DateTimeOffset(2007, 11, 4, 1, 30, 0, new TimeSpan(-8, 0, 0));           
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto));
 Console.WriteLine(dto.LocalDateTime);
 // If run on 3/8/2007 at 4:56 PM, the code produces the following
 // output:
 //    3/8/2007 4:56:03 PM
 //    3/8/2007 4:56:03 PM
 //    3/11/2007 3:30:00 AM
 //    3/11/2007 1:30:00 AM
 //    True
 //    3/11/2007 3:30:00 AM
 //    True
 //    11/4/2007 1:30:00 AM
 //    11/4/2007 1:30:00 AM
 //    True
 //    11/4/2007 1:30:00 AM      
 Dim dto As DateTimeOffset

 ' Current time
 dto = DateTimeOffset.Now
 Console.WriteLine(dto.LocalDateTime)
 ' UTC time
 dto = DateTimeOffset.UtcNow
 Console.WriteLine(dto.LocalDateTime)

' Transition to DST in local time zone occurs on 3/11/2007 at 2:00 AM
 dto = New DateTimeOffset(#03/11/2007 3:30AM#, New Timespan(-7, 0, 0))      
 Console.WriteLine(dto.LocalDateTime)
 dto = New DateTimeOffset(#03/11/2007 2:30AM#, New Timespan(-7, 0, 0))
 Console.WriteLine(dto.LocalDateTime)
 ' Invalid time in local time zone
 dto = New DateTimeOffset(#03/11/2007 2:30AM#, New Timespan(-8, 0, 0))
 Console.WriteLine(TimeZoneInfo.Local.IsInvalidTime(dto.DateTime))
 Console.WriteLine(dto.LocalDateTime)

 ' Transition from DST in local time zone occurs on 11/4/07 at 2:00 AM
 ' This is an ambiguous time
 dto = New DateTimeOffset(#11/4/2007 1:30AM#, New TimeSpan(-7, 0, 0))
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto))
 Console.WriteLine(dto.LocalDateTime)
 dto = New DateTimeOffset(#11/4/2007 2:30AM#, New TimeSpan(-7, 0, 0))           
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto))
 Console.WriteLine(dto.LocalDateTime)
 ' This is also an ambiguous time
 dto = New DateTimeOffset(#11/4/2007 1:30AM#, New TimeSpan(-8, 0, 0))           
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto))
 Console.WriteLine(dto.LocalDateTime)
 ' If run on 3/8/2007 at 4:56 PM, the code produces the following
 ' output:
 '    3/8/2007 4:56:03 PM
 '    3/8/2007 4:56:03 PM
 '    3/11/2007 3:30:00 AM
 '    3/11/2007 1:30:00 AM
 '    True
 '    3/11/2007 3:30:00 AM
 '    True
 '    11/4/2007 1:30:00 AM
 '    11/4/2007 1:30:00 AM
 '    True
 '    11/4/2007 1:30:00 AM      

注解

如有必要,LocalDateTime 属性会将当前 DateTimeOffset 对象的日期和时间转换为本地系统的日期和时间。If necessary, the LocalDateTime property converts the current DateTimeOffset object's date and time to the local system's date and time. 转换是一个两步操作:The conversion is a two-step operation:

  1. 属性将当前 DateTimeOffset 对象的时间转换为协调世界时(UTC)。The property converts the current DateTimeOffset object's time to Coordinated Universal Time (UTC).

  2. 然后,属性将 UTC 转换为本地时间。The property then converts UTC to local time.

没有无效的时间,不明确的时间将映射到本地区域的标准时间。There are no invalid times and ambiguous times are mapped to the local zone's standard time. (但是,转换可能会产生异常:如果从本地计算机派生的 DateTimeOffset 值反映了不明确的日期和时间,则可以将该值转换为 UTC,然后返回到与原始时间不同的本地时间。) 当执行此转换时,属性将在本地时区中应用任何调整规则。(However, the conversion can create an anomaly: if a DateTimeOffset value that is derived from the local computer reflects an ambiguous date and time, that value can be converted to UTC and then back to a local time that differs from the original time.) The property applies any adjustment rules in the local time zone when it performs this conversion.

此属性同时返回 DateTimeOffset 对象的日期和时间部分,这使 DateTimeOffset DateTime 转换非常有用。This property returns both the date and the time component of a DateTimeOffset object, which makes it useful for DateTimeOffset to DateTime conversion. 除了执行任何必要的时间转换以外,此属性还可以通过将返回的 DateTime 对象的 Kind 属性的值设置为 DateTimeKind.Local来与 DateTime 属性不同。In addition to performing any necessary time conversion, this property differs from the DateTime property by setting the value of the Kind property of the returned DateTime object to DateTimeKind.Local.

适用于

另请参阅