System.TimeZone.ToUniversalTime 方法

方法描述

返回对应于指定时间的协调世界时 (UTC)。

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

public virtual DateTime ToUniversalTime(
	DateTime time
)

参数/返回值

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

提示和注释

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

注意

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

如果 time 参数为不明确的时间,则该方法假定其为标准时间。 (不明确的时间是指可在本地时区中映射为标准时间或夏时制的时间。)如果 time 为无效时间,则该方法只从本地时区的 UTC 偏移量中减去本地时间,以返回 UTC。 (无效时间是指因应用夏时制调整规则而不存在的时间。)

由于相对于当前系统上的当前时区对 time 进行解释,因此如果在不同计算机上或者同一计算机上的不同时区下运行应用程序,则该方法返回的日期和时间可能会有所不同。 对于日期和时间值必须表示单一、明确时间点的情况,请使用 DateTimeOffset 值来表示本地时间。

ToUniversalTime 方法对应于 TimeZoneInfo.ConvertTimeToUtc(DateTime) 方法,重载的 DateTime 参数的 Kind 属性不等于 DateTimeKind.Utc。 应尽可能使用 TimeZoneInfo.ConvertTimeToUtc(DateTime) 方重载。

System.TimeZone.ToUniversalTime 方法例子

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

// Example of the TimeZone.IsDaylightSavingTime( DateTime ),
// TimeZone.IsDaylightSavingTime( DateTime, DaylightTime ), and 
// TimeZone.ToUniversalTime( DateTime ) methods.
using System;
using System.Globalization;

class DaylightTimeDemo
{
    static void Main( )
    {
        const string headFmt = "{0,-22}{1,-10}{2,-10}{3,-10}{4}";

        // Get the local time zone and a base local time.
        TimeZone localZone = TimeZone.CurrentTimeZone;
        DateTime localTime = new DateTime( 2001, 1, 1 );

        Console.WriteLine( "This example of \n" +
            "   TimeZone.IsDaylightSavingTime( DateTime ), \n" +
            "   TimeZone.IsDaylightSavingTime( " +
            "DateTime, DaylightTime ), and \n" +
            "   TimeZone.ToUniversalTime( DateTime )\n" +
            "generates the following output, which varies " +
            "depending on the \ntime zone in which it is run.\n" );
        Console.WriteLine( "The example creates several local " +
            "times and the corresponding \nCoordinated Universal " +
            "Times (UTC) and shows if they occur in \ndaylight " +
            "saving time (DST), both for specified years " +
            "and for \nany year.\n" );
        Console.WriteLine( "Local time: {0}\n", 
            localZone.StandardName );

        Console.WriteLine( headFmt, "Local Date and Time", "2001 DST?", 
            "2002 DST?", "Any DST?", "Corresponding UTC" );
        Console.WriteLine( headFmt, "-------------------", "---------", 
            "---------", "--------", "-----------------" );

        // Create DaylightTime objects for specific years.
        DaylightTime daylight2001 = 
            localZone.GetDaylightChanges( 2001 );
        DaylightTime daylight2002 = 
            localZone.GetDaylightChanges( 2002 );

        // Generate several local times.
        for( int loopX = 0; loopX <= 10; loopX++ )
        {
            // Calculate the corresponding UTC.
            DateTime utcTime = 
                localZone.ToUniversalTime( localTime );

            // Show if dates and times occur in daylight saving 
            // time, for specified years and for any year.
            Console.WriteLine( "{0,-22:yyyy-MM-dd HH:mm}" +
                "{1,-10}{2,-10}{3,-10}{4:yyyy-MM-dd HH:mm}", 
                localTime, 
                TimeZone.IsDaylightSavingTime( 
                    localTime, daylight2001 ), 
                TimeZone.IsDaylightSavingTime( 
                    localTime, daylight2002 ), 
                localZone.IsDaylightSavingTime( localTime ), 
                utcTime );

            // Advance to another local time.
            localTime = localTime.AddDays( 109.1 );
        }
    } 
} 

/*
This example of
   TimeZone.IsDaylightSavingTime( DateTime ),
   TimeZone.IsDaylightSavingTime( DateTime, DaylightTime ), and
   TimeZone.ToUniversalTime( DateTime )
generates the following output, which varies depending on the
time zone in which it is run.

The example creates several local times and the corresponding
Coordinated Universal Times (UTC) and shows if they occur in
daylight saving time (DST), both for specified years and for
any year.

Local time: Pacific Standard Time

Local Date and Time   2001 DST? 2002 DST? Any DST?  Corresponding UTC
-------------------   --------- --------- --------  -----------------
2001-01-01 00:00      False     False     False     2001-01-01 08:00
2001-04-20 02:24      True      False     True      2001-04-20 09:24
2001-08-07 04:48      True      False     True      2001-08-07 11:48
2001-11-24 07:12      False     False     False     2001-11-24 15:12
2002-03-13 09:36      False     False     False     2002-03-13 17:36
2002-06-30 12:00      False     True      True      2002-06-30 19:00
2002-10-17 14:24      False     True      True      2002-10-17 21:24
2003-02-03 16:48      False     False     False     2003-02-04 00:48
2003-05-23 19:12      False     False     True      2003-05-24 02:12
2003-09-09 21:36      False     False     True      2003-09-10 04:36
2003-12-28 00:00      False     False     False     2003-12-28 08:00
*/

异常

异常 异常描述

命名空间

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 系统要求。