DateTimeFormatInfo.DateSeparator 屬性

定義

取得或設定分隔日期組成部分 (即年、月和日) 的字串。Gets or sets the string that separates the components of a date, that is, the year, month, and day.

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

屬性值

分隔日期組成部分 (即年、月和日) 的字串。The string that separates the components of a date, that is, the year, month, and day. 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 文化特性的物件、將其日期分隔符號變更為 "-",並使用 "d"、"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 "d", "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);
      
      string[] formats = { "d", "G", "g" };
      CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
      DateTimeFormatInfo dtfi = culture.DateTimeFormat;
      dtfi.DateSeparator = "-";
      
      foreach (var fmt in formats)
         Console.WriteLine("{0}: {1}", fmt, value.ToString(fmt, dtfi));
   }
}
// The example displays the following output:
//       d: 9-8-2013
//       G: 9-8-2013 12:00:00 AM
//       g: 9-8-2013 12:00 AM
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim value As New Date(2013, 9, 8)
      
      Dim formats() As String = { "d", "G", "g" }
      Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture("en-US")
      Dim dtfi As DateTimeFormatInfo = culture.DateTimeFormat
      dtfi.DateSeparator = "-"
      
      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:
'       d: 9-8-2013
'       G: 9-8-2013 12:00:00 AM
'       g: 9-8-2013 12:00 AM

備註

如果自訂格式字串包含 "/" 格式規範,則DateTime.ToString方法會顯示的DateSeparator值,以取代結果字串中的 "/"。If a custom format string includes the "/" format specifier, the DateTime.ToString method displays the value of DateSeparator in place of the "/" in the result string.

DateSeparator屬性會定義字串,以取代格式化作業中的結果字串中的日期分隔符號("/" 自訂日期和時間格式規範)。The DateSeparator property defines the string that replaces the date separator ("/" custom date and time format specifier) in a result string in a formatting operation. 它也會在剖析作業中定義日期分隔符號字串。It also defines the date separator string in a parsing operation.

適用於

另請參閱