DateTimeFormatInfo.DateSeparator 屬性

定義

取得或設定分隔日期組成部分 (即年、月和日) 的字串。

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

屬性值

分隔日期組成部分 (即年、月和日) 的字串。 InvariantInfo 的預設值為 "/"。

例外狀況

此屬性設定為 null

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

範例

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

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

屬性 DateSeparator 會定義字串,以在格式化作業的結果字串中取代日期分隔符號 (「/」 自訂日期和時間格式規範) 。 它也會在剖析作業中定義日期分隔符號字串。

適用於

另請參閱