TimeZoneInfo.IsInvalidTime(DateTime) Method

Definition

指示特定日期和时间是否无效。Indicates whether a particular date and time is invalid.

public:
 bool IsInvalidTime(DateTime dateTime);
public bool IsInvalidTime (DateTime dateTime);
member this.IsInvalidTime : DateTime -> bool
Public Function IsInvalidTime (dateTime As DateTime) As Boolean

Parameters

dateTime
DateTime

日期和时间值。A date and time value.

Returns

Boolean

如果 dateTime 无效,则为 true;否则为 falsetrue if dateTime is invalid; otherwise, false.

Examples

太平洋时区的夏令时开始时间为凌晨2:00。In the Pacific Time zone, daylight saving time begins at 2:00 A.M. 2006年4月2日。on April 2, 2006. 下面的代码以一分钟的时间间隔从凌晨1:59 传递时间。The following code passes the time at one-minute intervals from 1:59 A.M. 2006年4月2日,上午3:01on April 2, 2006, to 3:01 A.M. 2006年4月2日,表示太平洋时区 TimeZoneInfo 对象的 IsInvalidTime 方法。on April 2, 2006, to the IsInvalidTime method of a TimeZoneInfo object that represents the Pacific Time zone. 控制台输出指示上午2:00 的所有时间。The console output indicates that all times from 2:00 A.M. 2006年4月2日,上午2:59on April 2, 2006, to 2:59 A.M. 2006年4月2日无效。on April 2, 2006, are invalid.

// Specify DateTimeKind in Date constructor
DateTime baseTime = new DateTime(2007, 3, 11, 1, 59, 0, DateTimeKind.Unspecified);
DateTime newTime;

// Get Pacific Standard Time zone
TimeZoneInfo pstZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");

// List possible invalid times for a 63-minute interval, from 1:59 AM to 3:01 AM
for (int ctr = 0; ctr < 63; ctr++)
{
   // Because of assignment, newTime.Kind is also DateTimeKind.Unspecified
   newTime = baseTime.AddMinutes(ctr);
   Console.WriteLine("{0} is invalid: {1}", newTime, pstZone.IsInvalidTime(newTime));
}
' Specify DateTimeKind in Date constructor
Dim baseTime As New Date(2007, 3, 11, 1, 59, 0, DateTimeKind.Unspecified)
Dim newTime As Date

' Get Pacific Standard Time zone
Dim pstZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")

' List possible invalid times for 63-minute interval, from 1:59 AM to 3:01 AM
For ctr As Integer = 0 To 62
   ' Because of assignment, newTime.Kind is also DateTimeKind.Unspecified
   newTime = baseTime.AddMinutes(ctr)
   Console.WriteLine("{0} is invalid: {1}", newTime, pstZone.IsInvalidTime(newTime))
Next

Remarks

由于应用了调整规则,当前时区的时间范围无效,因此无法映射到协调世界时(UTC)。An invalid time falls within a range of times for the current time zone that cannot be mapped to Coordinated Universal Time (UTC) due to the application of an adjustment rule. 通常,当时间向前移动夏令时时,会发生无效的时间。Typically, invalid times occur when the time moves ahead for daylight saving time. 有关说明,请参阅 "示例" 部分。See the Example section for an illustration.

dateTime 参数的 Kind 属性的值会影响 dateTime 是否表示无效时间,如下表所示。The value of the Kind property of the dateTime parameter affects whether dateTime represents an invalid time, as the following table shows.

DateTime 属性DateTime.Kind property TimeZoneInfo 对象(如果适用)TimeZoneInfo object (if applicable) 行为Behavior
DateTimeKind.Local TimeZoneInfo.Local 确定时间是否无效。Determines whether the time is invalid.
DateTimeKind.Local TimeZoneInfo.Utc 或非本地时区。TimeZoneInfo.Utc or a non-local time zone. dateTime 转换为 TimeZoneInfo 对象的时间,并返回 falseConverts dateTime to the time of the TimeZoneInfo object and returns false.
DateTimeKind.Unspecified 不适用。Not applicable. 假定 dateTimeTimeZoneInfo 对象的时间,并确定它是否无效。Assumes dateTime is the time of the TimeZoneInfo object and determines whether it is invalid.
DateTimeKind.Utc 不适用。Not applicable. 返回 falseReturns false.

Applies to

See also