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

DateTime.ToLongTimeString 方法

2013/12/13

将当前 DateTime 对象的值转换为其等效的长时间字符串表示形式。

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

public string ToLongTimeString()

返回值

类型: System.String
当前 DateTime 对象的长时间字符串表示形式。

当前 DateTime 对象的值使用与当前线程区域性关联的 LongTimePattern 属性所定义的模式进行格式设置。返回值与通过使用 ToString(String) 方法指定“T”标准日期和时间格式字符串返回的值相同。

重要说明重要说明:

ToLongTimeString 方法所返回的字符串区分区域性。它反映当前区域性的 DateTimeFormatInfo 对象所定义的模式。例如,对于 en-US 区域性,标准长时间模式为“h:mm:ss tt”;对于 de-DE 区域性,该模式为“HH:mm:ss”;对于 ja-JP 区域性,该模式为“H:mm:ss”。此外,也可以在特定计算机上自定义特定格式字符串,以便该字符串不同于标准长时间格式字符串。

有关更改与格式字符关联的格式模式的更多信息,请参见 DateTimeFormatInfo 类。

下面的示例说明 ToLongTimeString 方法。


// This code example demonstrates the DateTime.ToLongDateString(),
// DateTime.ToLongTimeString(), DateTime.ToShortDateString(), and
// DateTime.ToShortTimeString() methods.

using System;
using System.Threading;
using System.Globalization;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string msg1 = "The date and time patterns are defined in the DateTimeFormatInfo \n" +
                    "object associated with the current thread culture.\n";

      // Initialize a DateTime object.
      outputBlock.Text += String.Format("Initialize the DateTime object to May 16, 2001 3:02:15 AM.\n") + "\n";
      DateTime myDateTime = new System.DateTime(2001, 5, 16, 3, 2, 15);

      // Identify the source of the date and time patterns.
      outputBlock.Text += msg1 + "\n";

      // Display the name of the current culture.
      CultureInfo ci = Thread.CurrentThread.CurrentCulture;
      outputBlock.Text += String.Format("Current culture: \"{0}\"\n", ci.Name) + "\n";

      // Display the long date pattern and string.
      outputBlock.Text += String.Format("Long date pattern: \"{0}\"", ci.DateTimeFormat.LongDatePattern) + "\n";
      outputBlock.Text += String.Format("Long date string:  \"{0}\"\n", myDateTime.ToLongDateString()) + "\n";

      // Display the long time pattern and string.
      outputBlock.Text += String.Format("Long time pattern: \"{0}\"", ci.DateTimeFormat.LongTimePattern) + "\n";
      outputBlock.Text += String.Format("Long time string:  \"{0}\"\n", myDateTime.ToLongTimeString()) + "\n";

      // Display the short date pattern and string.
      outputBlock.Text += String.Format("Short date pattern: \"{0}\"", ci.DateTimeFormat.ShortDatePattern) + "\n";
      outputBlock.Text += String.Format("Short date string:  \"{0}\"\n", myDateTime.ToShortDateString()) + "\n";

      // Display the short time pattern and string.
      outputBlock.Text += String.Format("Short time pattern: \"{0}\"", ci.DateTimeFormat.ShortTimePattern) + "\n";
      outputBlock.Text += String.Format("Short time string:  \"{0}\"\n", myDateTime.ToShortTimeString()) + "\n";
   }
}

/*
This code example produces the following results:

Initialize the DateTime object to May 16, 2001 3:02:15 AM

The date and time patterns are defined in the DateTimeFormatInfo
object associated with the current thread culture.

Current culture: "en-US"

Long date pattern: "dddd, MMMM dd, yyyy"
Long date string:  "Wednesday, May 16, 2001"

Long time pattern: "h:mm:ss tt"
Long time string:  "3:02:15 AM"

Short date pattern: "M/d/yyyy"
Short date string:  "5/16/2001"

Short time pattern: "h:mm tt"
Short time string:  "3:02 AM"

*/


Windows Phone OS

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

Windows Phone

显示:
© 2015 Microsoft