Partager via


TimeZoneInfo.IsInvalidTime Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Indicates whether a particular date and time is invalid.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Function IsInvalidTime ( _
    dateTime As DateTime _
) As Boolean
public bool IsInvalidTime(
    DateTime dateTime
)

Parameters

Return Value

Type: System.Boolean
true if dateTime is invalid; otherwise, false.

Remarks

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.

The value of the Kind property of the dateTime parameter affects whether dateTime represents an invalid time, as the following table shows.

DateTime.Kind property

TimeZoneInfo object (if applicable)

Behavior

DateTimeKind.Local

TimeZoneInfo.Local

Determines whether the time is invalid.

DateTimeKind.Local

TimeZoneInfo.Utc or a non-local time zone.

Converts dateTime to the time of the TimeZoneInfo object and returns false.

DateTimeKind.Unspecified

Not applicable.

Assumes dateTime is the time of the TimeZoneInfo object and determines whether it is invalid.

DateTimeKind.Utc

Not applicable.

Returns false.

Version Notes

XNA Framework

 When this method is used in the XNA Framework, it throws a NotSupportedException exception.

Examples

In the Pacific Standard Time zone, which is the local time zone on the computer on which this example runs, daylight saving time begins at 2:00 A.M. on April 2, 2006. The following code passes the time at one-minute intervals from 1:59 A.M. on April 2, 2006, to 3:01 A.M. on April 2, 2006, to the IsInvalidTime method of a TimeZoneInfo object that represents the Pacific Standard Time zone. The console output indicates that all times from 2:00 A.M. on April 2, 2006, to 2:59 A.M. on April 2, 2006, are invalid.

' Specify DateTimeKind in Date constructor
Dim baseTime As New Date(2007, 3, 11, 1, 59, 0, DateTimeKind.Unspecified)
Dim newTime As Date

' List possible invalid times for 63-minute interval, from 1:59 AM to 3:01 AM
' This assumes the local time zone is U.S. Pacific Standard Time.
For ctr As Integer = 0 To 62
   ' Because of assignment, newTime.Kind is also DateTimeKind.Unspecified
   newTime = baseTime.AddMinutes(ctr)
   outputBlock.Text &= String.Format("{0} is invalid: {1}", newTime, _
                       TimeZoneInfo.Local.IsInvalidTime(newTime)) & vbCrLf
Next
// Specify DateTimeKind in Date constructor
DateTime baseTime = new DateTime(2007, 3, 11, 1, 59, 0, DateTimeKind.Unspecified);
DateTime newTime;

// List possible invalid times for a 63-minute interval, from 1:59 AM to 3:01 AM
// This assumes the local time zone is U.S. Pacific Standard Time.
for (int ctr = 0; ctr < 63; ctr++)
{
   // Because of assignment, newTime.Kind is also DateTimeKind.Unspecified
   newTime = baseTime.AddMinutes(ctr);
   outputBlock.Text += String.Format("{0} is invalid: {1}", newTime, 
                       TimeZoneInfo.Local.IsInvalidTime(newTime)) + "\n";
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.