使用 Windows.Globalization.DateTimeFormatting 命名空間中的類別搭配自定義範本和模式,以完全符合您想要的格式來顯示日期和時間。
簡介
DateTimeFormatter 類別提供各種方式,為世界各地的語言和區域正確格式化日期和時間。 您可以針對年份、月、日等使用標準格式。 或者,您可以將格式範本傳遞至 DateTimeFormatter 建構函式的 formatTemplate 自變數,例如 “longdate” 或 “month day”。
但是,當您想要更充分掌控您想要顯示之 DateTime 物件的元件順序和格式時,您可以將格式模式傳遞至建構函式的 formatTemplate 自變數。 格式模式會使用特殊語法,可讓您取得 DateTime 物件的個別元件,例如月份名稱,或只取得年份值,以便以您選擇的任何自定義格式顯示它們。 此外,模式可以當地語系化以適應其他語言和區域。
注意 這只是格式樣式的概述。 如需對格式範本和格式模式的更完整討論,請參閱 DateTimeFormatter 類別的備註部分。
格式範本和格式模式之間的差異
格式範本是不受文化影響的格式字串。 因此,如果您使用格式範本建構 DateTimeFormatter,則格式器會以目前語言的正確順序顯示您的格式元件。 相反地,格式模式是特定文化特性。 如果您使用格式模式來建構 DateTimeFormatter,則格式器會使用完全相同的模式。 因此,一個模式不一定在各文化中都適用。
讓我們使用範例來說明此差異。 我們會將簡單的格式範本(非模式)傳遞至 DateTimeFormatter 建構函式。 這是範本 「month day」 的格式。
var dateFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("month day");
這會根據目前內容的語言和區域值來建立格式器。 格式範本中的元件順序並不重要;格式器會以目前語言的正確順序顯示它們。 因此,它會顯示英文(美國)的“January 1”、法文(法國)的“1 janvier”和日文的“1月1日”。
另一方面,格式模式是依文化而定。 讓我們存取我們格式範本的樣式模式。
IReadOnlyList<string> monthDayPatterns = dateFormatter.Patterns;
這會根據運行時間語言和區域產生不同的結果。 不同的區域可能會使用不同的元件、以不同的順序,有或沒有其他字元和間距。
En-US: "{month.full} {day.integer}"
Fr-FR: "{day.integer} {month.full}"
Ja-JP: "{month.integer}月{day.integer}日"
在上述範例中,我們輸入了一個不受文化影響的格式字串,結果得到一個依語言和地區而定的格式字串(這取決於我們在呼叫 dateFormatter.Patterns時所處的語言和區域)。 因此,如果您從特定文化特性格式模式建構 DateTimeFormatter,則它只適用於特定語言/區域。
var dateFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("{month.full} {day.integer}");
上述格式子會針對括號 {}內的個別元件傳回文化特定的值。 但是,格式模式中的元件順序是不變的。 您會精準地得到您要求的內容,但這可能符合文化,或可能不符合。 此格式器適用於英文(美國),但不適用於法文(法國)或日文。
En-US: January 1
Fr-FR: janvier 1 (inappropriate for France; non-standard order)
Ja-JP: 1月1 (inappropriate for Japan; the day symbol 日 is missing)
此外,目前正確的模式未來可能不正確。 國家或地區可能會變更其行事曆系統,這會改變格式範本。 Windows 會根據格式範本更新格式化工具的輸出,以配合這類變更。 因此,您應該只在其中一或多個條件下使用模式語法。
- 您不依賴於特定格式的輸出。
- 您不需要格式遵循某些特定文化的標準。
- 您特別想要讓模式不因文化特性而異。
- 您想要本地化實際的格式模式字串本身。
以下是格式範本和格式模式之間的差異摘要。
格式範本,例如「月日」
- DateTime 格式的抽象化表示,其中包含月份、日期等值,以任何順序排列。
- 保證會跨 Windows 支援的所有語言區域值傳回有效的標準格式。
- 保證會為指定的語言區域提供文化上適當的格式化字串。
- 並非所有元件的組合都是有效的。 例如,“dayofweek day” 無效。
格式模式,例如 “{month.full} {day.integer}”
- 明確排序的字串,表示完整月份名稱,後面接著空格,後面接著日整數,依該順序,或任何您指定的特定格式模式。
- 可能無法對應至任何語言區域配對的有效標準格式。
- 不保證在文化上是適當的。
- 可以依任何順序指定元件的任何組合。
範例
假設您想要以特定格式顯示目前月份和日期與目前時間。 例如,您希望美國英文使用者看到類似如下的內容:
June 25 | 1:38 PM
日期部分會對應至「月日」格式範本,而時間部分則對應至「小時分鐘」格式範本。 因此,您可以建構相關日期和時間格式範本的格式器,然後使用可本地化的格式字串將其輸出串連在一起。
var dateToFormat = System.DateTime.Now;
var resourceLoader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();
var dateFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("month day");
var timeFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("hour minute");
var date = dateFormatter.Format(dateToFormat);
var time = timeFormatter.Format(dateToFormat);
string output = string.Format(resourceLoader.GetString("CustomDateTimeFormatString"), date, time);
CustomDateTimeFormatString 是資源檔 (.resw) 中可本地化資源的資源標識碼。 對於英文的默認語言(美國),這會設定為 “{0} |{1}“以及批注,指出”{0}“是日期,而”{1}“是時間。 如此一來,翻譯員就可以視需要調整格式項目。 例如,如果在某些語言或地區將時間置於日期之前顯得更自然,他們可以變更項目的順序。 或者,他們可以將「|」替換成其他分隔符號。
實作此範例的另一種方法是查詢兩個格式器的格式模式,將這些模式串連在一起,然後以結果格式模式構建第三個格式器。
var resourceLoader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();
var dateFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("month day");
var timeFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("hour minute");
string dateFormatterPattern = dateFormatter.Patterns[0];
string timeFormatterPattern = timeFormatter.Patterns[0];
string pattern = string.Format(resourceLoader.GetString("CustomDateTimeFormatString"), dateFormatterPattern, timeFormatterPattern);
var patternFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter(pattern);
string output = patternFormatter.Format(System.DateTime.Now);