System.TimeZone.ToLocalTime 方法

方法描述

返回对应于指定日期和时间值的本地时间。

语法定义(C# System.TimeZone.ToLocalTime 方法 的用法)

public virtual DateTime ToLocalTime(
	DateTime time
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
time System-DateTime 协调世界时 (UTC) 时间。
返回值 System.DateTime 一个 DateTime 对象,其值为对应于 time 的本地时间。

提示和注释

下表显示了 time 参数与该方法返回的 DateTime 值之间的关系。

time 参数

行为

返回值

协调世界时 (UTC) 时间 (DateTimeKind.Utc)。

将时间从 UTC 转换为本地时间。

一个 DateTime 对象,其值为对应于 time 的本地时间。

本地时间 (DateTimeKind.Local)。

不需要进行转换。

由 time 参数表示的同一 DateTime 值。

未指定的时间 (DateTimeKind.Unspecified)。

假定时间为 UTC 并将其从 UTC 转换为本地时间。

一个 DateTime 对象,其值为对应于 time 的本地时间。

如果本地时区遵守夏时制,则执行转换时,ToLocalTime 会将当前调整规则应用于 time。

注意

ToLocalTime 方法只识别本地时区的当前夏时制调整规则。 因此,只有在最新调整规则有效期间才能保证准确返回对应于特定 UTC 时间的本地时间。 如果 time 为遵守先前调整规则的历史日期和时间值,则可能会返回不准确的结果。

ToLocalTime 方法对应于 TimeZoneInfo.ConvertTimeFromUtc 方法,其 destinationTimeZone 参数设置为 TimeZoneInfo.Local。 应尽可能使用 TimeZoneInfo.ConvertTimeFromUtc 方法。

对继承者的说明

尽管不是必须,但大多数情况下在 .NET Framework 2.0 版上运行的派生类应重写该方法的默认实现。 在 .NET Framework 1.0 和 1.1 版中,ToLocalTime 方法调用 GetUtcOffset 方法,并在返回本地时间时调整为夏时制时间。 然而,从 .NET Framework 2.0 开始,默认实现的行为取决于 time 参数的 Kind 属性。 如果其值为 DateTimeKind.Local,则该方法返回未更改的 time。 如果其值为 DateTimeKind.Utc 或 DateTimeKind.Unspecified,则该方法假定 time 为 UTC 并将其转换为本地系统时间,而不调用 GetUtcOffset 方法。

下面的代码提供 ToLocalTime 方法的默认实现的简单重写。 在该代码中,internalTimeZone 变量表示 TimeZone 类的专用实例:

C#

VB

复制

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

}

}

System.TimeZone.ToLocalTime 方法例子

下面的示例使用 ToLocalTime 方法,返回与数个协调世界时 (UTC) 时间相对应的本地时间。

// Example of the TimeZone.ToLocalTime( DateTime ) and 
// TimeZone.GetUtcOffset( DateTime ) methods.
using System;

class UTCTimeDemo
{
    static void Main( )
    {
        const string headFmt = "{0,-20}{1,-20}{2,-12}{3}";

        // Get the local time zone and a base Coordinated Universal 
        // Time (UTC).
        TimeZone localZone = TimeZone.CurrentTimeZone;
        DateTime baseUTC = new DateTime( 2000, 1, 1 );

        Console.WriteLine( "This example of \n" +
            "   TimeZone.ToLocalTime( DateTime ) and\n" +
            "   TimeZone.GetUtcOffset( DateTime ) \ngenerates the " +
            "following output, which varies depending on the time " +
            "zone \nin which it is run. The example creates several " +
            "Coordinated Universal \nTimes (UTC), displays the " +
            "corresponding local times and UTC offsets, \nand shows " +
            "if the times occur in daylight saving time (DST)." );
        Console.WriteLine( "\nLocal time: {0}\n", 
            localZone.StandardName );

        Console.WriteLine( headFmt, "UTC", "Local Time", 
            " Offset", "DST?" );
        Console.WriteLine( headFmt, "---", "----------", 
            " ------", "----" );

        // Generate several UTC times.
        for( int loopX = 0; loopX <= 10; loopX++ )
        {
            // Calculate the local time and UTC offset.
            DateTime localTime = localZone.ToLocalTime( baseUTC );
            TimeSpan localOffset = 
                localZone.GetUtcOffset( localTime );

            Console.WriteLine( "{0,-20:yyyy-MM-dd HH:mm}" +
                "{1,-20:yyyy-MM-dd HH:mm}{2,-12}{3}", 
                baseUTC, localTime, localOffset, 
                localZone.IsDaylightSavingTime( localTime ) );

            // Advance to another UTC.
            baseUTC = baseUTC.AddDays( 155.55 );
        }
    } 
} 

/*
This example of
   TimeZone.ToLocalTime( DateTime ) and
   TimeZone.GetUtcOffset( DateTime )
generates the following output, which varies depending on the time zone
in which it is run. The example creates several Coordinated Universal
Times (UTC), displays the corresponding local times and UTC offsets,
and shows if the times occur in daylight saving time (DST).

Local time: Pacific Standard Time

UTC                 Local Time           Offset     DST?
---                 ----------           ------     ----
2000-01-01 00:00    1999-12-31 16:00    -08:00:00   False
2000-06-04 13:12    2000-06-04 06:12    -07:00:00   True
2000-11-07 02:24    2000-11-06 18:24    -08:00:00   False
2001-04-11 15:36    2001-04-11 08:36    -07:00:00   True
2001-09-14 04:48    2001-09-13 21:48    -07:00:00   True
2002-02-16 18:00    2002-02-16 10:00    -08:00:00   False
2002-07-22 07:12    2002-07-22 00:12    -07:00:00   True
2002-12-24 20:24    2002-12-24 12:24    -08:00:00   False
2003-05-29 09:36    2003-05-29 02:36    -07:00:00   True
2003-10-31 22:48    2003-10-31 14:48    -08:00:00   False
2004-04-04 12:00    2004-04-04 05:00    -07:00:00   True
*/

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。