DateTimePicker.CustomFormat 屬性

定義

取得或設定自訂的日期時間格式字串。

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

屬性值

String

表示自訂日期時間格式的字串。 預設為 null

範例

下列程式碼範例會設定 CustomFormat 屬性,讓 DateTimePicker 將日期顯示為 「2001 年 6 月 1 日 - 星期五」。 此程式碼假設 已在 上 Form 建立 控制項的 DateTimePicker 實例。

public:
   void SetMyCustomFormat()
   {
      // Set the Format type and the CustomFormat string.
      dateTimePicker1->Format = DateTimePickerFormat::Custom;
      dateTimePicker1->CustomFormat = "MMMM dd, yyyy - dddd";
   }
public void SetMyCustomFormat()
{
   // Set the Format type and the CustomFormat string.
   dateTimePicker1.Format = DateTimePickerFormat.Custom;
   dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";
}
Public Sub SetMyCustomFormat()
    ' Set the Format type and the CustomFormat string.
    dateTimePicker1.Format = DateTimePickerFormat.Custom
    dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd"
End Sub

備註

若要顯示包含日期和時間分隔符號的字串常值或格式化字串,您必須在子字串中使用逸出字元。 例如,若要將日期顯示為 「June 15 at 12:00 PM」,請將 CustomFormat 屬性設定為 「MMMM dd 'at' t:mm tt」。 如果 「at」 子字串未以逸出字元括住,則結果為 「June 15 aP 12:00PM」,因為 「t」 字元會讀取為一個字母 A.M./P.M。 格式字串 (請參閱) 下方的格式字串資料表。

格式字串可以結合以格式化日期和時間。 例如,若要將日期和時間顯示為 06/15/2001 12:00 PM,此屬性應該設定為 「MM'/'dd'/'yyyy hh':'mm tt」。 如需詳細資訊,請參閱 自訂日期和時間格式字串

注意

此屬性 Format 必須設定 DateTimePickerFormat.Custom 為 ,這個屬性才能影響顯示日期和時間的格式。

下表列出所有有效的格式字串及其描述。

格式字串 描述
d 一或兩位數的日期。
dd 兩位數的日期。 單一位數的日值前面會加上 0。
ddd 星期三字元縮寫。
dddd 星期四的完整名稱。
h 12 小時格式的一或兩位數小時。
hh 12 小時格式的兩位數小時。 單一數位值前面會加上 0。
H 24 小時格式的一或兩位數小時。
HH 24 小時格式的兩位數小時。 單一數位值前面會加上 0。
m 一或兩位數的分鐘數。
mm 兩位數的分鐘。 單一數位值前面會加上 0。
M 一位數或兩位數的月份數位。
MM 兩位數月份數位。 單一數位值前面會加上 0。
MMM 三個字元月份縮寫。
MMMM 完整月份名稱。
s 一或兩位數秒。
ss 兩位數秒。 單一數位值前面會加上 0。
t 一個字母 A.M./P.M. A.M. (縮寫 會顯示為 「A」) 。
tt 雙字母 A.M./P.M. 縮寫 (A.M. 會顯示為 「AM」) 。
y 2001 (1 位數年份會顯示為 「1」) 。
yy (2001 年最後兩位數會顯示為 「01」) 。
yyyy 2001 年 (的完整年份會顯示為 「2001」) 。

適用於

另請參閱