TimeZoneInfo.IsInvalidTime(DateTime) 方法

定義

表示特定日期和時間是否無效。

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

參數

dateTime
DateTime

日期和時間值。

傳回

Boolean

如果 dateTime 無效則為 true,否則為 false

範例

在太平洋時區中,日光節約時間從上午 2:00 開始。 于 2006 年 4 月 2 日。 下列程式碼會以一分鐘間隔從上午 1:59 傳遞時間。 2006 年 4 月 2 日到上午 3:01 在 2006 年 4 月 2 日,指向 IsInvalidTime 代表太平洋時區的物件 TimeZoneInfo 方法。 主控台輸出指出從上午 2:00 開始的所有時間。 于 2006 年 4 月 2 日到上午 2:59 在 2006 年 4 月 2 日無效。

// 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
let baseTime = DateTime(2007, 3, 11, 1, 59, 0, DateTimeKind.Unspecified)

// Get Pacific Standard Time zone
let pstZone = TimeZoneInfo.FindSystemTimeZoneById "Pacific Standard Time"

// List possible invalid times for a 63-minute interval, from 1:59 AM to 3:01 AM
for i = 0 to 62 do
    // Because of assignment, newTime.Kind is also DateTimeKind.Unspecified
    let newTime = baseTime.AddMinutes i
    printfn $"{newTime} is invalid: {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

備註

不正確時間落在目前時區的某個時間範圍內,因為套用調整規則,所以無法對應到國際標準時間 (UTC) 。 通常,當時間往前移動以日光節約時間時,就會發生不正確時間。 如需圖例,請參閱一節。

參數的 Kind dateTime 屬性值會影響是否 dateTime 代表不正確時間,如下表所示。

DateTime.Kind 屬性 如果適用) ,timeZoneInfo 物件 ( 行為
DateTimeKind.Local TimeZoneInfo.Local 判斷時間是否無效。
DateTimeKind.Local TimeZoneInfo.Utc 或非當地時區。 dateTime轉換為 物件的時間 TimeZoneInfo ,並傳 false 回 。
DateTimeKind.Unspecified 不適用。 dateTime假設 是 物件的時間, TimeZoneInfo 並判斷物件是否無效。
DateTimeKind.Utc 不適用。 傳回 false

適用於

另請參閱