DateTime::ToLocalTime Method

Converts the value of the current DateTime object to local time.

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

No code example is currently available or this language may not be supported.

Return Value

Type: System::DateTime
An object whose Kind property is Local, and whose value is one of the following: the local time equivalent of the current DateTime value, or DateTime::MaxValue if the converted value is too large to be represented by a DateTime object, or DateTime::MinValue if the converted value is too small to be represented as a DateTime object.

The local time is equal to the Coordinated Universal Time (UTC) time plus the UTC offset. The conversion also takes into account the daylight saving time rule that applies to the time represented by the current DateTime object.

Important noteImportant Note:

The ToLocalTime method recognizes only the current adjustment rule when converting from UTC to local time. As a result, conversions for periods before the current adjustment rule came into effect may not accurately reflect the difference between UTC and local time.

The value returned by the ToLocalTime method is determined by the Kind property of the current DateTime object. The following table describes the possible results.

Kind property value

Result

Utc

This instance of DateTime is converted to local time.

Local

No conversion is performed.

Unspecified

This instance of DateTime is assumed to be a UTC time, and the conversion is performed as if Kind were Utc.

The value returned by the conversion is a DateTime whose Kind property always returns Local. Consequently, a valid result is returned even if ToLocalTime is applied repeatedly to the same DateTime.

Notes to Callers

You can use the ToLocalTime method to restore a local date and time value that was converted to UTC by the ToUniversalTime or FromFileTimeUtc method. However, if the original time represents an invalid time in the local time zone, it will not match the restored value. When the ToLocalTime method converts a time from UTC to the local time zone, it also adjusts the time so that is valid in the local time zone.

For example, the transition from standard time to daylight saving time occurs in the U.S. Pacific Standard Time zone on March 14, 2010, at 2:00 A.M., when the time advances by one hour, to 3:00 AM. This hour interval is an invalid time, that is, a time interval that does not exist in this time zone. The following example shows that when a time that falls within this range is converted to UTC by the ToUniversalTime method and is then restored by the ToLocalTime method, the original value is adjusted to become a valid time. You can determine whether a particular date and time value may be subject to modification by passing it to the TimeZoneInfo::IsInvalidTime method, as the example illustrates.

No code example is currently available or this language may not be supported.

The following example demonstrates the ToLocalTime method.

No code example is currently available or this language may not be supported.

The following example uses the SpecifyKind method to demonstrate how the Kind property influences the ToLocalTime and ToUniversalTime conversion methods.

No code example is currently available or this language may not be supported.

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

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

Community Additions

ADD
Show: