DateTimeFormatInfo.TimeSeparator 屬性

定義

取得或設定分隔時間組成部分 (即時、分和秒) 的字串。Gets or sets the string that separates the components of time, that is, the hour, minutes, and seconds.

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

屬性值

分隔時間組成部分的字串。The string that separates the components of time. InvariantInfo 的預設值為 ":"。The default for InvariantInfo is ":".

例外狀況

正在將屬性設定為 nullThe property is being set to null.

正在設定屬性,而且 DateTimeFormatInfo 物件為唯讀。The property is being set and the DateTimeFormatInfo object is read-only.

範例

下列範例會具現CultureInfo化 en-us 文化特性的物件、將其日期分隔符號變更為 ".",並使用 "t"、"t"、"f"、"f"、"g" 和 "g" 標準格式字串來顯示日期。The following example instantiates a CultureInfo object for the en-US culture, changes its date separator to ".", and displays a date by using the "t", "T", "F", "f", "G", and "g" standard format strings.

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值,以取代格式模式中的 ":"。If the custom pattern includes the format pattern ":", DateTime.ToString displays the value of TimeSeparator in place of the ":" in the format pattern.

時間分隔符號是從ShortTimePattern屬性衍生而來。The time separator is derived from the ShortTimePattern property. 我們建議您將時間分隔符號以短或長時間模式設定為精確的字串,而不是使用時間分隔符號預留位置。We recommend that you set the time separator in short or long time patterns to an exact string instead of using the time separator placeholder. 例如,若要取得模式 h-mm-ss,請將模式設定為 "h-mm-ss"。For example, to obtain the pattern h-mm-ss, set the pattern to "h-mm-ss". 這種作法也可讓您設定包含多種分隔符號類型的 "h'h ' mm ' ss '" (3h 36m 12s)模式。This practice also enables you to set patterns such as "h'h 'mm'm 'ss's'" (3h 36m 12s) that include multiple types of separators. TimeSeparator屬性會定義字串,以取代格式化作業中的結果字串中的時間分隔符號(":" 自訂日期和時間格式規範)。The TimeSeparator property defines the string that replaces the time separator (":" custom date and time format specifier) in a result string in a formatting operation. 它也會在剖析作業中定義時間分隔符號字串。It also defines the time separator string in a parsing operation.

適用於

另請參閱