TimeZoneInfo.IsInvalidTime(DateTime) Method

Definition

Indicates whether a particular date and time is invalid.

C#
public bool IsInvalidTime(DateTime dateTime);

Parameters

dateTime
DateTime

A date and time value.

Returns

true if dateTime is invalid; otherwise, false.

Examples

In the Pacific Time zone, 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 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.

C#
// 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));
}

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.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

See also