Прочетете на английски Редактиране

Споделяне чрез


TimeZone.ToLocalTime(DateTime) Method

Definition

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

C#
public virtual DateTime ToLocalTime(DateTime time);

Parameters

time
DateTime

A Coordinated Universal Time (UTC) time.

Returns

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

Remarks

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.

Бележка

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(DateTime) 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 Local, this method returns time unchanged. If its value is either Utc or Unspecified, this method assumes time is UTC and converts it to the local system time without calling the GetUtcOffset(DateTime) 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:

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

Applies to

Продукт Версии
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 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 2.0, 2.1

See also