DateTime::ToLocalTime Method
Converts the value of the current DateTime object to local time.
Assembly: mscorlib (in mscorlib.dll)
Return Value
Type: System::DateTimeAn object whose Kind property is Local, and whose value is the local time equivalent to the value of the current DateTime object, or DateTime::MaxValue if the converted value is too large to be represented by a DateTime object, or 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. For more information about the UTC offset, see TimeZone::GetUtcOffset. The conversion also takes into account the daylight saving time rule that applies to the time represented by the current DateTime object.
Important |
|---|
On Windows XP systems, 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. |
Starting with the .NET Framework version 2.0, 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 | Results |
|---|---|
Utc | This instance of DateTime is converted to local time. |
Local | No conversion is performed. |
This instance of DateTime is assumed to be a UTC time, and the conversion is performed as if Kind were Utc. |
Note |
|---|
The ToLocalTime method converts a DateTime value from UTC to local time. To convert the time in any designated time zone to local time, use the TimeZoneInfo::ConvertTime method. |
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 CallersYou 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 Time zone on March 14, 2010, at 2:00 A.M., when the time advances by one hour, to 3:00 A.M. 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.
The following example demonstrates the ToLocalTime method.
System::Console::WriteLine( "Enter a date and time." ); String^ strDateTime = System::Console::ReadLine(); System::DateTime localDateTime; try { localDateTime = System::DateTime::Parse( strDateTime ); } catch ( System::FormatException^ ) { System::Console::WriteLine( "Invalid format." ); return; } System::DateTime univDateTime = localDateTime.ToUniversalTime(); System::Console::WriteLine( "{0} local time is {1} universal time.", localDateTime, univDateTime ); System::Console::WriteLine( "Enter a date and time in universal time." ); strDateTime = System::Console::ReadLine(); try { univDateTime = System::DateTime::Parse( strDateTime ); } catch ( System::FormatException^ ) { System::Console::WriteLine( "Invalid format." ); return; } localDateTime = univDateTime.ToLocalTime(); System::Console::WriteLine( "{0} universal time is {1} local time.", univDateTime, localDateTime );
The following example uses the SpecifyKind method to demonstrate how the Kind property influences the ToLocalTime and ToUniversalTime conversion methods.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Important