此代碼在每個 Console.WriteLine 陳述句下方的注釋中生成輸出。
誰能解釋這種行為?
DateTime date1 = new DateTime(2008, 8, 18);
Console.WriteLine(date1.ToString("M"));
// Displays August 18
Console.WriteLine(date1.ToString("M "));
// Displays 8
Console.WriteLine(date1.ToString("(M)"));
// Displays (8)
Console.WriteLine(date1.ToString("(M) MMM, MMMM"));
// Displays (8) Aug, August
uj5u.com熱心網友回復:
誰能解釋這種行為?
是的,它完全記錄在標準日期和時間格式字串和自定義日期和時間格式字串中。
讓我們一次一個地瀏覽它們:
date1.ToString("M")
:使用單個字符“M”,因此它是“月/日模式”的標準格式字串date1.ToString("M ")
:它使用一個帶有兩個字符的格式字串,所以它是一個自定義格式字串,它使用M
的是月份編號,1-12 沒有填充。date1.ToString("(M)")
: 同樣,自定義格式字串使用M
date1.ToString("(M) MMM, MMMM")
: 自定義格式字串,使用M
, 以及MMM
("abbreviated name of the month") 和MMMM
(" full name of the month")
前兩者之間的重要區別在于,格式字串僅在為單個字符時才被視為標準格式。否則,它被認為是自定義格式。如果您想單獨使用單字符自定義格式說明符,您可以將%
其用作前導字符 - 因此date1.ToString("%M")
將回傳“8”而不是“August 18”。
uj5u.com熱心網友回復:
C# 中的日期和時間由 C# 中的 DateTime 結構處理,該結構提供屬性和方法來格式化不同日期時間格式的日期。
M-> Month number(eg.3)
MM-> Month number with leading zero(eg.04)
MMM-> Abbreviated Month Name (e.g. Dec)
MMMM-> Full month name (e.g. December)
https://www.c-sharpcorner.com/blogs/date-and-time-format-in-c-sharp-programming1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/470805.html