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

DateTime.SpecifyKind 方法

2013/12/13

创建新的 DateTime 对象,该对象具有与指定的 DateTime 相同的刻度数,但是根据指定的 DateTimeKind 值的指示,指定为本地时间或协调世界时 (UTC),或者两者皆否。

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

public static DateTime SpecifyKind(
	DateTime value,
	DateTimeKind kind
)

参数

value
类型: System.DateTime
新对象的日期和时间值。
kind
类型: System.DateTimeKind
枚举值之一,指示新对象是按本地时间还是 UTC 指定,还是两者皆否。

返回值

类型: System.DateTime
一个新对象,它具有与 value 参数表示的对象相同的刻度数以及由 kind 参数指定的 DateTimeKind 值。

DateTime 对象由一个 Kind 字段和一个 Ticks 字段组成,Kind 字段指示该时间值是基于本地时间、协调世界时 (UTC) 还是两者皆否,而 Ticks 字段包含以 100 纳秒计时周期为单位计量的时间值。SpecifyKind 方法使用指定的 kind 参数和初始时间值创建新的 DateTime 对象。

重要说明重要说明:

返回的 DateTime 值不会将相同的时刻表示为 value 参数,SpecifyKind 不是一个时区转换方法。相反,它将 value 参数所指定的时间保留不变,并将 Kind 属性设置为 kind

如果您接收了未指定 Kind 字段的 DateTime 对象,但是您可以通过独立的方式确定 Ticks 字段是表示本地时区还是 UTC,在这种互操作性情况下,SpecifyKind 方法很有用。

下面的示例使用 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

显示:
© 2014 Microsoft