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

DateTime.ToLocalTime 方法

2013/12/13

将当前 DateTime 对象的值转换为本地时间。

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

public DateTime ToLocalTime()

返回值

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

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

重要说明重要说明:

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

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

Kind 属性值

结果

Utc

DateTime 的此实例被转换为本地时间。

Local

未执行任何转换。

Unspecified

假定 DateTime 的此实例为 UTC 时间,并且执行转换时将 Kind 视作 Utc

该转换返回的值为 Kind 属性总是返回 LocalDateTime。因此,即使 ToLocalTime 反复应用于同一个 DateTime,返回的仍然是有效结果。

对调用者的说明

您可以使用 ToLocalTime 方法还原由 ToUniversalTimeFromFileTimeUtc 方法转换为 UTC 的本地日期和时间值。但是,如果原始时间表示本地时区中的一个无效的时间,它将不匹配已还原的值。当 ToLocalTime 方法将时间从 UTC 时间转换为本地时区时间时,还可以调整时间,使其成为有效的在本地时区时间。

例如,从标准时间到夏时制的切换在美国太平洋标准时区的 2010 年 3 月 14 日凌晨 2:00 发生,此时时间前进一个小时,成为凌晨 3:00。此时间间隔为无效时间,即,此时区中不存在的时间间隔。下面的示例显示了位于此范围内的时间通过 ToUniversalTime 方法转换为 UTC,然后在通过 ToLocalTime 方法还原时,原始值将调整为有效的时间。您可以通过将特定日期和时间值传递到 TimeZoneInfo.IsInvalidTime 方法确定是否对其进行修改,如示例所阐释。


using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      DateTime date1 = new DateTime(2010, 3, 14, 2, 30, 0, DateTimeKind.Local);
      outputBlock.Text += String.Format("Invalid time: {0}",
                        TimeZoneInfo.Local.IsInvalidTime(date1)) + "\n";
      DateTime utcDate1 = date1.ToUniversalTime();
      DateTime date2 = utcDate1.ToLocalTime();
      outputBlock.Text += String.Format("{0} --> {1}", date1, date2) + "\n";
   }
}
// The example displays the following output:
//       Invalid time: True
//       3/14/2010 2:30:00 AM --> 3/14/2010 3:30:00 AM


下面的示例说明 ToLocalTime 方法。


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