Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

TimeZone.ToLocalTime Method

Returns the local time that corresponds to a specified date and time value.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public virtual DateTime ToLocalTime(
	DateTime time
)

Parameters

time
Type: System.DateTime

A Coordinated Universal Time (UTC) time.

Return Value

Type: System.DateTime
A DateTime object whose value is the local time that corresponds to time.

The following table shows the relationship between the time parameter and the DateTime value returned by this method.

time parameter

Behavior

Return value

A Coordinated Universal Time (UTC) time (DateTimeKind.Utc).

Converts the time from UTC to the local time.

A DateTime object whose value is the local time that corresponds to time.

A local time (DateTimeKind.Local).

No conversion necessary.

The same DateTime value represented by the time parameter.

An unspecified time (DateTimeKind.Unspecified).

Assumes that the time is UTC and converts it from UTC to the local time.

A DateTime object whose value is the local time that corresponds to time.

If the local time zone observes daylight saving time, ToLocalTime applies the current adjustment rule to time when performing the conversion.

NoteNote

   The ToLocalTime method recognizes only the current daylight saving time adjustment rule for the local time zone. As a result, it is guaranteed to accurately return the local time corresponding to a particular UTC time only during the period in which the latest adjustment rule is in effect. It may return inaccurate results if time is a historic date and time value that was subject to a previous adjustment rule.

The ToLocalTime method corresponds to the TimeZoneInfo.ConvertTimeFromUtc method with its destinationTimeZone parameter set to TimeZoneInfo.Local. Whenever possible, use the TimeZoneInfo.ConvertTimeFromUtc method.

Notes to Inheritors

Although it is not required, in most cases derived classes running under the .NET Framework version 2.0 should override the default implementation of this method. In the .NET Framework versions 1.0 and 1.1, the ToLocalTime method called the GetUtcOffset method and adjusted for daylight saving time when returning the local time. However, starting with the .NET Framework 2.0, the behavior of the default implementation depends on the Kind property of the time parameter. If its value is DateTimeKind.Local, this method returns time unchanged. If its value is either DateTimeKind.Utc or DateTimeKind.Unspecified, this method assumes time is UTC and converts it to the local system time without calling the GetUtcOffset method.

The following code provides a simple override of the default implementation of the ToLocalTime method. In this code, the internalTimeZone variable represents a private instance of the TimeZone class:

public override DateTime ToLocalTime(DateTime time)
{
   if (time.Kind == DateTimeKind.Local)
   {
      return time;
   }
   else if (time.Kind == DateTimeKind.Utc)
   {
      DateTime returnTime = new DateTime(time.Ticks, DateTimeKind.Local);
      returnTime += this.GetUtcOffset(returnTime);
      if (internalTimeZone.IsDaylightSavingTime(returnTime))
         returnTime -= new TimeSpan(1, 0, 0);
      return returnTime;
   }      
   else
   {
      throw new ArgumentException("The source time zone cannot be determined.");
   }      
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft. All rights reserved.