TimeZoneInfo.IsInvalidTime(DateTime) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Anger om ett visst datum och en viss tid är ogiltig.
public:
bool IsInvalidTime(DateTime dateTime);
public bool IsInvalidTime(DateTime dateTime);
member this.IsInvalidTime : DateTime -> bool
Public Function IsInvalidTime (dateTime As DateTime) As Boolean
Parametrar
- dateTime
- DateTime
Ett datum- och tidsvärde.
Returer
true om dateTime är ogiltigt, annars false.
Exempel
I Stillahavstidszonen börjar sommartiden kl. 02.00 den 2 april 2006. Följande kod skickar tiden med en minuts intervall från 01:59 den 2 april 2006 till 03:01 den 2 april 2006 till metoden för IsInvalidTime ett TimeZoneInfo objekt som representerar Tidszon i Stillahavsområdet. Konsolens utdata anger att alla tider från 02:00 den 2 april 2006 till 02:59 den 2 april 2006 är ogiltiga.
// 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
Kommentarer
En ogiltig tid ligger inom ett tidsintervall för den aktuella tidszonen som inte kan mappas till Coordinated Universal Time (UTC) på grund av tillämpningen av en justeringsregel. Normalt inträffar ogiltiga tider när tiden går framåt för sommartid. Se avsnittet Exempel för en bild.
Värdet för egenskapen för Kind parametern dateTime påverkar om dateTime representerar en ogiltig tid, som följande tabell visar.
| Egenskapen DateTime.Kind | TimeZoneInfo-objekt (om tillämpligt) | Behavior |
|---|---|---|
| DateTimeKind.Local | TimeZoneInfo.Local | Avgör om tiden är ogiltig. |
| DateTimeKind.Local | TimeZoneInfo.Utc eller en icke-lokal tidszon. | Konverterar dateTime till objektets TimeZoneInfo tid och returnerar false. |
| DateTimeKind.Unspecified | Ej tillämpbart. | Förutsätter att det är objektets dateTimeTimeZoneInfo tid och avgör om det är ogiltigt. |
| DateTimeKind.Utc | Ej tillämpbart. | Returnerar false. |