导出 (0) 打印
全部展开
信息
您所需的主题如下所示。但此主题未包含在此库中。

DateTime.ToUniversalTime 方法

2013/12/13

将当前 DateTime 对象的值转换为协调世界时 (UTC)。

Namespace:  System
程序集:  mscorlib(位于 mscorlib.dll 中)

public DateTime ToUniversalTime()

返回值

类型: System.DateTime
一个对象,其 Kind 属性为 Utc,并且其值是下列值之一:等效于当前 DateTime 值的 UTC;如果经转换的值过大以至于不能由 DateTime 对象表示,则为 DateTime.MaxValue,或者,如果经转换的值过小以至于不能表示为 DateTime 对象,则为 DateTime.MinValue

协调世界时 (UTC) 等于本地时间减去 UTC 偏移量。转换还要考虑应用于当前 DateTime 对象所表示的时间的夏时制规则。

重要说明重要说明:

ToUniversalTime 方法只识别从本地时间转换为 UTC 时的当前调整规则。因此,当前调整规则生效之前的转换可能不能准确反映本地时间和 UTC 之间的差值。

ToUniversalTime 方法返回的值是由当前 DateTime 对象的 Kind 属性决定的。下表说明了可能的结果。

Kind

结果

Utc

未执行任何转换。

Local

当前 DateTime 对象被转换 UTC。

Unspecified

假定当前 DateTime 对象为本地时间,并且执行转换时将 Kind 视作 Local

说明注意:

ToUniversalTime 方法将 DateTime 值从本地时间转换为 UTC。若要转换与 UTC 的偏移量已知的时间,请使用 ToUniversalTime 方法。

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

对调用者的说明

有时可使用 ToUniversalTime 方法将本地时间转换为 UTC,随后再通过调用 ToLocalTime 方法来恢复。但是,如果原始时间表示本地时区中的一个无效的时间,这两个当地时间值将会不相等。有关更多信息和一个示例,请参见 ToLocalTime 方法。

下面的示例说明 ToUniversalTime 方法。


outputBlock.Text += "Enter a date and time." + "\n";
string strDateTime = System.Console.ReadLine();

System.DateTime localDateTime;
try
{
   localDateTime = System.DateTime.Parse(strDateTime);
}
catch (System.FormatException)
{
   outputBlock.Text += "Invalid format." + "\n";
   return;
}

System.DateTime univDateTime = localDateTime.ToUniversalTime();

outputBlock.Text += String.Format("{0} local time is {1} universal time.",
                   localDateTime,
                   univDateTime) + "\n";

outputBlock.Text += "Enter a date and time in universal time." + "\n";
strDateTime = System.Console.ReadLine();

try
{
   univDateTime = System.DateTime.Parse(strDateTime);
}
catch (System.FormatException)
{
   outputBlock.Text += "Invalid format." + "\n";
   return;
}

localDateTime = univDateTime.ToLocalTime();

outputBlock.Text += String.Format("{0} universal time is {1} local time.",
                   univDateTime,
                   localDateTime) + "\n";


下面的示例使用 SpecifyKind 方法演示 Kind 属性如何影响 ToLocalTimeToUniversalTime 转换方法。


// This code example demonstrates the DateTime Kind, Now, and
// UtcNow properties, and the SpecifyKind(), ToLocalTime(), 
// and ToUniversalTime() methods.

using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Get the date and time for the current moment, adjusted 
      // to the local time zone.

      DateTime saveNow = DateTime.Now;

      // Get the date and time for the current moment expressed 
      // as coordinated universal time (UTC).

      DateTime saveUtcNow = DateTime.UtcNow;
      DateTime myDt;

      // Display the value and Kind property of the current moment 
      // expressed as UTC and local time.

      DisplayNow(outputBlock, "UtcNow: ..........", saveUtcNow);
      DisplayNow(outputBlock, "Now: .............", saveNow);
      outputBlock.Text += "\n";

      // Change the Kind property of the current moment to 
      // DateTimeKind.Utc and display the result.

      myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Utc);
      Display(outputBlock, "Utc: .............", myDt);

      // Change the Kind property of the current moment to 
      // DateTimeKind.Local and display the result.

      myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Local);
      Display(outputBlock, "Local: ...........", myDt);

      // Change the Kind property of the current moment to 
      // DateTimeKind.Unspecified and display the result.

      myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Unspecified);
      Display(outputBlock, "Unspecified: .....", myDt);
   }

   // Display the value and Kind property of a DateTime structure, the 
   // DateTime structure converted to local time, and the DateTime 
   // structure converted to universal time. 

   public static string datePatt = @"M/d/yyyy hh:mm:ss tt";
   public static void Display(System.Windows.Controls.TextBlock outputBlock, string title, DateTime inputDt)
   {
      DateTime dispDt = inputDt;
      string dtString;

      // Display the original DateTime.

      dtString = dispDt.ToString(datePatt);
      outputBlock.Text += String.Format("{0} {1}, Kind = {2}",
                        title, dtString, dispDt.Kind) + "\n";

      // Convert inputDt to local time and display the result. 
      // If inputDt.Kind is DateTimeKind.Utc, the conversion is performed.
      // If inputDt.Kind is DateTimeKind.Local, the conversion is not performed.
      // If inputDt.Kind is DateTimeKind.Unspecified, the conversion is 
      // performed as if inputDt was universal time.

      dispDt = inputDt.ToLocalTime();
      dtString = dispDt.ToString(datePatt);
      outputBlock.Text += String.Format("  ToLocalTime:     {0}, Kind = {1}",
                        dtString, dispDt.Kind) + "\n";

      // Convert inputDt to universal time and display the result. 
      // If inputDt.Kind is DateTimeKind.Utc, the conversion is not performed.
      // If inputDt.Kind is DateTimeKind.Local, the conversion is performed.
      // If inputDt.Kind is DateTimeKind.Unspecified, the conversion is 
      // performed as if inputDt was local time.

      dispDt = inputDt.ToUniversalTime();
      dtString = dispDt.ToString(datePatt);
      outputBlock.Text += String.Format("  ToUniversalTime: {0}, Kind = {1}",
                        dtString, dispDt.Kind) + "\n";
      outputBlock.Text += "\n";
   }

   // Display the value and Kind property for DateTime.Now and DateTime.UtcNow.

   public static void DisplayNow(System.Windows.Controls.TextBlock outputBlock, string title, DateTime inputDt)
   {
      string dtString = inputDt.ToString(datePatt);
      outputBlock.Text += String.Format("{0} {1}, Kind = {2}",
                        title, dtString, inputDt.Kind) + "\n";
   }
}

/*
This code example produces the following results:

UtcNow: .......... 5/6/2005 09:34:42 PM, Kind = Utc
Now: ............. 5/6/2005 02:34:42 PM, Kind = Local

Utc: ............. 5/6/2005 02:34:42 PM, Kind = Utc
  ToLocalTime:     5/6/2005 07:34:42 AM, Kind = Local
  ToUniversalTime: 5/6/2005 02:34:42 PM, Kind = Utc

Local: ........... 5/6/2005 02:34:42 PM, Kind = Local
  ToLocalTime:     5/6/2005 02:34:42 PM, Kind = Local
  ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc

Unspecified: ..... 5/6/2005 02:34:42 PM, Kind = Unspecified
  ToLocalTime:     5/6/2005 07:34:42 AM, Kind = Local
  ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc

*/


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

Microsoft 正在进行一项网上调查,以了解您对 MSDN 网站的意见。 如果您选择参加,我们将会在您离开 MSDN 网站时向您显示该网上调查。

是否要参加?
显示:
© 2015 Microsoft