DateTimeFormatInfo.TimeSeparator 屬性

定義

取得或設定分隔時間組成部分 (即時、分和秒) 的字串。

public:
 property System::String ^ TimeSeparator { System::String ^ get(); void set(System::String ^ value); };
public string TimeSeparator { get; set; }
member this.TimeSeparator : string with get, set
Public Property TimeSeparator As String

屬性值

分隔時間組成部分的字串。 InvariantInfo 的預設值為 ":"。

例外狀況

此屬性設定為 null

正在設定屬性,而且 DateTimeFormatInfo 物件為唯讀。

範例

下列範例會具現化 CultureInfo en-US 文化特性的物件、將其日期分隔符號變更為 「.」,並使用 「t」、「T」、「F」、「f」、「G」 和 「g」 標準格式字串來顯示日期。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      DateTime value = new DateTime(2013, 9, 8, 14, 30, 0);

      string[] formats = { "t", "T", "f", "F", "G", "g" };
      CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
      DateTimeFormatInfo dtfi = culture.DateTimeFormat;
      dtfi.TimeSeparator = ".";

      foreach (var fmt in formats)
         Console.WriteLine("{0}: {1}", fmt, value.ToString(fmt, dtfi));
   }
}
// The example displays the following output:
//       t: 2.30 PM
//       T: 2.30.00 PM
//       f: Sunday, September 08, 2013 2.30 PM
//       F: Sunday, September 08, 2013 2.30.00 PM
//       G: 9/8/2013 2.30.00 PM
//       g: 9/8/2013 2.30 PM
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim value As New Date(2013, 9, 8, 14, 30, 0)
      
      Dim formats() As String = { "t", "T", "f", "F", "G", "g" }
      Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture("en-US")
      Dim dtfi As DateTimeFormatInfo = culture.DateTimeFormat
      dtfi.TimeSeparator = "."
      
      For Each fmt In formats
         Console.WriteLine("{0}: {1}", fmt, value.ToString(fmt, dtfi))
      Next      
   End Sub
End Module
' The example displays the following output:
'       t: 2.30 PM
'       T: 2.30.00 PM
'       f: Sunday, September 08, 2013 2.30 PM
'       F: Sunday, September 08, 2013 2.30.00 PM
'       G: 9/8/2013 2.30.00 PM
'       g: 9/8/2013 2.30 PM

備註

如果自訂模式包含格式模式 「:」, DateTime.ToString 則會以格式模式顯示 取代 「:」 的值 TimeSeparator

注意

標準格式模式,例如 FullDateTimePattern ,不一定使用 「:」。 使用這些模式時,變更 TimeSeparator 可能沒有作用。

時間分隔符號衍生自 ShortTimePattern 屬性。 我們建議您將短時間或長時間模式中的時間分隔符號設定為確切字串,而不是使用時間分隔符號預留位置。 例如,若要取得模式 h-mm-ss,請將模式設定為 「h-mm-ss」。 此做法也可讓您設定模式,例如包含多個分隔符號類型的 「h'h 'mm'm 'ss'」 (36m 12s) 。 屬性 TimeSeparator 會定義字串,以取代格式化作業中結果字串中 (「:」 自訂日期和時間格式規範) 的時間分隔符號。 它也會在剖析作業中定義時間分隔符號字串。

適用於

另請參閱