DateTimeFormatInfo.GetAllDateTimePatterns 方法 - Microsoft ...

文章推薦指數: 80 %
投票人數:10人

DateTimeFormat.GetAllDateTimePatterns()) { total += 1; if (! DateTime. ... M.yy H.mm') // Unable to parse 01.2.11 07.30 (format 'dd.M.yy HH.mm') // Unable ... 跳到主要內容 已不再支援此瀏覽器。

請升級至MicrosoftEdge,以利用最新功能、安全性更新和技術支援。

下載MicrosoftEdge 其他資訊 目錄 結束焦點模式 語言 閱讀英文 儲存 編輯 共用 Twitter LinkedIn Facebook 電子郵件 WeChat 目錄 語言 語言 閱讀英文 儲存 目錄 閱讀英文 儲存 編輯 Twitter LinkedIn Facebook 電子郵件 WeChat 目錄 DateTimeFormatInfo.GetAllDateTimePatterns方法 參考 此頁面有所助益嗎? Yes No 還有其他意見反應嗎? 系統會將意見反應傳送給Microsoft:按下[提交]按鈕,您的意見反應將用來改善Microsoft產品和服務。

隱私權原則。

送出 謝謝。

定義 命名空間: System.Globalization 組件:System.Runtime.dll 組件:mscorlib.dll 組件:netstandard.dll 重要 部分資訊涉及發行前產品,在發行之前可能會有大幅修改。

Microsoft對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。

傳回可格式化日期和時間值的標準模式。

本文內容 多載 GetAllDateTimePatterns() 傳回可格式化日期和時間值的所有標準模式。

GetAllDateTimePatterns(Char) 傳回可以使用所指定標準格式字串來格式化日期和時間值的所有模式。

GetAllDateTimePatterns() 傳回可格式化日期和時間值的所有標準模式。

public: cli::array<:string>^GetAllDateTimePatterns(); publicstring[]GetAllDateTimePatterns(); memberthis.GetAllDateTimePatterns:unit->string[] PublicFunctionGetAllDateTimePatterns()AsString() 傳回 String[] 陣列,其中包含可格式化日期和時間值的標準模式。

範例 下列範例會顯示不因文化特性而異的日期和時間格式字串,以及使用該格式字串來格式化特定日期時所產生的結果字串。

usingSystem; usingSystem.Globalization; publicclassExample { publicstaticvoidMain() { DateTimedate=newDateTime(2014,8,28,12,28,30); DateTimeFormatInfoinvDTF=newDateTimeFormatInfo(); String[]formats=invDTF.GetAllDateTimePatterns(); Console.WriteLine("{0,-40}{1}\n","Pattern","ResultString"); foreach(varfmtinformats) Console.WriteLine("{0,-40}{1}",fmt,date.ToString(fmt)); } } //Theexampledisplaysthefollowingoutput: //PatternResultString // //MM/dd/yyyy08/28/2014 //yyyy-MM-dd2014-08-28 //dddd,ddMMMMyyyyThursday,28August2014 //dddd,ddMMMMyyyyHH:mmThursday,28August201412:28 //dddd,ddMMMMyyyyhh:mmttThursday,28August201412:28PM //dddd,ddMMMMyyyyH:mmThursday,28August201412:28 //dddd,ddMMMMyyyyh:mmttThursday,28August201412:28PM //dddd,ddMMMMyyyyHH:mm:ssThursday,28August201412:28:30 //MM/dd/yyyyHH:mm08/28/201412:28 //MM/dd/yyyyhh:mmtt08/28/201412:28PM //MM/dd/yyyyH:mm08/28/201412:28 //MM/dd/yyyyh:mmtt08/28/201412:28PM //yyyy-MM-ddHH:mm2014-08-2812:28 //yyyy-MM-ddhh:mmtt2014-08-2812:28PM //yyyy-MM-ddH:mm2014-08-2812:28 //yyyy-MM-ddh:mmtt2014-08-2812:28PM //MM/dd/yyyyHH:mm:ss08/28/201412:28:30 //yyyy-MM-ddHH:mm:ss2014-08-2812:28:30 //MMMMddAugust28 //MMMMddAugust28 //yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK2014-08-28T12:28:30.0000000 //yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK2014-08-28T12:28:30.0000000 //ddd,ddMMMyyyyHH':'mm':'ss'GMT'Thu,28Aug201412:28:30GMT //ddd,ddMMMyyyyHH':'mm':'ss'GMT'Thu,28Aug201412:28:30GMT //yyyy'-'MM'-'dd'T'HH':'mm':'ss2014-08-28T12:28:30 //HH:mm12:28 //hh:mmtt12:28PM //H:mm12:28 //h:mmtt12:28PM //HH:mm:ss12:28:30 //yyyy'-'MM'-'ddHH':'mm':'ss'Z'2014-08-2812:28:30Z //dddd,ddMMMMyyyyHH:mm:ssThursday,28August201412:28:30 //yyyyMMMM2014August //yyyyMMMM2014August ImportsSystem.Globalization ModuleExample PublicSubMain() Dimdate1AsDate=#08/28/201412:28:30PM# DiminvDTFAsNewDateTimeFormatInfo() Dimformats()AsString=invDTF.GetAllDateTimePatterns() Console.WriteLine("{0,-40}{1}","Pattern","ResultString") Console.WriteLine() ForEachfmtInformats Console.WriteLine("{0,-40}{1}",fmt,date1.ToString(fmt)) Next EndSub EndModule 'Theexampledisplaysthefollowingoutput: 'PatternResultString ' 'MM/dd/yyyy08/28/2014 'yyyy-MM-dd2014-08-28 'dddd,ddMMMMyyyyThursday,28August2014 'dddd,ddMMMMyyyyHH:mmThursday,28August201412:28 'dddd,ddMMMMyyyyhh:mmttThursday,28August201412:28PM 'dddd,ddMMMMyyyyH:mmThursday,28August201412:28 'dddd,ddMMMMyyyyh:mmttThursday,28August201412:28PM 'dddd,ddMMMMyyyyHH:mm:ssThursday,28August201412:28:30 'MM/dd/yyyyHH:mm08/28/201412:28 'MM/dd/yyyyhh:mmtt08/28/201412:28PM 'MM/dd/yyyyH:mm08/28/201412:28 'MM/dd/yyyyh:mmtt08/28/201412:28PM 'yyyy-MM-ddHH:mm2014-08-2812:28 'yyyy-MM-ddhh:mmtt2014-08-2812:28PM 'yyyy-MM-ddH:mm2014-08-2812:28 'yyyy-MM-ddh:mmtt2014-08-2812:28PM 'MM/dd/yyyyHH:mm:ss08/28/201412:28:30 'yyyy-MM-ddHH:mm:ss2014-08-2812:28:30 'MMMMddAugust28 'MMMMddAugust28 'yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK2014-08-28T12:28:30.0000000 'yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK2014-08-28T12:28:30.0000000 'ddd,ddMMMyyyyHH':'mm':'ss'GMT'Thu,28Aug201412:28:30GMT 'ddd,ddMMMyyyyHH':'mm':'ss'GMT'Thu,28Aug201412:28:30GMT 'yyyy'-'MM'-'dd'T'HH':'mm':'ss2014-08-28T12:28:30 'HH:mm12:28 'hh:mmtt12:28PM 'H:mm12:28 'h:mmtt12:28PM 'HH:mm:ss12:28:30 'yyyy'-'MM'-'ddHH':'mm':'ss'Z'2014-08-2812:28:30Z 'dddd,ddMMMMyyyyHH:mm:ssThursday,28August201412:28:30 'yyyyMMMM2014August 'yyyyMMMM2014August 此範例會藉由呼叫函式來具現化代表不因文化特性而異DateTimeFormatInfo的物件DateTimeFormatInfo。

它也可以DateTimeFormatInfo從屬性中取出代表不因文化特性而異的InvariantInfo。

備註 這個方法會傳回陣列,其中包含所有對應于標準格式字串的自訂格式字串。

請參閱標準日期和時間格式字串,以取得標準格式字串的清單。

您可以GetAllDateTimePatterns在格式化作業中,于方法所傳回的陣列中使用自訂格式字串。

但是,如果您這樣做,則在該格式化作業中傳回之日期和時間值的字串表示,無法一律由Parse和方法成功剖析TryParse。

因此,您無法假設方法所傳回的自訂格式字串GetAllDateTimePatterns可以用來來回行程日期和時間值。

下列範例說明此問題。

它會抓取DateTimeFormatInfo包含義大利(義大利文)文化特性之格式資訊的物件。

它會將方法所傳回之陣列中的每個自訂格式字串傳遞GetAllDateTimePatterns()給DateTime.ToString(String)方法,以建立日期和時間的字串表示。

然後,此範例會藉由呼叫方法,嘗試剖析此值DateTime.TryParse(String,DateTime)。

如範例的輸出所示,某些自訂格式字串不會產生成功往返的日期和時間值。

usingSystem; usingSystem.Globalization; publicclassExample { publicstaticvoidMain() { CultureInfoculture=CultureInfo.CreateSpecificCulture("it-IT"); DateTimedate1=newDateTime(2011,02,01,7,30,45,0); DateTimedate2; inttotal=0; intnoRoundTrip=0; foreach(varfmtinculture.DateTimeFormat.GetAllDateTimePatterns()){ total+=1; if(!DateTime.TryParse(date1.ToString(fmt),outdate2)){ noRoundTrip++; Console.WriteLine("Unabletoparse{0:"+fmt+"}(format'{1}')", date1,fmt); } } Console.WriteLine("\nUnabletoround-trip{0}of{1}formatstrings.", noRoundTrip,total); } } //Theexampledisplaysthefollowingoutput: //UnabletoparseTuesday1February20117.30(format'dddddMMMMyyyyH.mm') //UnabletoparseTuesday1February201107.30(format'dddddMMMMyyyyHH.mm') //Unabletoparse1-Feb-117.30(format'd-MMM-yyH.mm') //Unabletoparse1-Feb-1107.30(format'd-MMM-yyHH.mm') //Unabletoparse1February20117.30(format'dMMMMyyyyH.mm') //Unabletoparse1February201107.30(format'dMMMMyyyyHH.mm') //UnabletoparseTuesday1February20117.30.45(format'dddddMMMMyyyyH.mm.ss') //UnabletoparseTuesday1February201107.30.45(format'dddddMMMMyyyyHH.mm.ss') //Unabletoparse1-Feb-117.30.45(format'd-MMM-yyH.mm.ss') //Unabletoparse1-Feb-1107.30.45(format'd-MMM-yyHH.mm.ss') //Unabletoparse1February20117.30.45(format'dMMMMyyyyH.mm.ss') //Unabletoparse1February201107.30.45(format'dMMMMyyyyHH.mm.ss') //Unabletoparse01/02/20117.30(format'dd/MM/yyyyH.mm') //Unabletoparse01/02/201107.30(format'dd/MM/yyyyHH.mm') //Unabletoparse01/Feb/20117.30(format'dd/MMM/yyyyH.mm') //Unabletoparse01/Feb/201107.30(format'dd/MMM/yyyyHH.mm') //Unabletoparse01/02/117.30(format'dd/MM/yyH.mm') //Unabletoparse01/02/1107.30(format'dd/MM/yyHH.mm') //Unabletoparse01.2.117.30(format'dd.M.yyH.mm') //Unabletoparse01.2.1107.30(format'dd.M.yyHH.mm') //Unabletoparse1/2/117.30(format'd/M/yyH.mm') //Unabletoparse1/2/1107.30(format'd/M/yyHH.mm') //Unabletoparse2011-02-017.30(format'yyyy-MM-ddH.mm') //Unabletoparse2011-02-0107.30(format'yyyy-MM-ddHH.mm') //Unabletoparse01/02/20117.30.45(format'dd/MM/yyyyH.mm.ss') //Unabletoparse01/02/201107.30.45(format'dd/MM/yyyyHH.mm.ss') //Unabletoparse01/Feb/20117.30.45(format'dd/MMM/yyyyH.mm.ss') //Unabletoparse01/Feb/201107.30.45(format'dd/MMM/yyyyHH.mm.ss') //Unabletoparse01/02/117.30.45(format'dd/MM/yyH.mm.ss') //Unabletoparse01/02/1107.30.45(format'dd/MM/yyHH.mm.ss') //Unabletoparse01.2.117.30.45(format'dd.M.yyH.mm.ss') //Unabletoparse01.2.1107.30.45(format'dd.M.yyHH.mm.ss') //Unabletoparse1/2/117.30.45(format'd/M/yyH.mm.ss') //Unabletoparse1/2/1107.30.45(format'd/M/yyHH.mm.ss') //Unabletoparse2011-02-017.30.45(format'yyyy-MM-ddH.mm.ss') //Unabletoparse2011-02-0107.30.45(format'yyyy-MM-ddHH.mm.ss') //UnabletoparseTuesday1February20117.30.45(format'dddddMMMMyyyyH.mm.ss') //UnabletoparseTuesday1February201107.30.45(format'dddddMMMMyyyyHH.mm.ss') //Unabletoparse1-Feb-117.30.45(format'd-MMM-yyH.mm.ss') //Unabletoparse1-Feb-1107.30.45(format'd-MMM-yyHH.mm.ss') //Unabletoparse1February20117.30.45(format'dMMMMyyyyH.mm.ss') //Unabletoparse1February201107.30.45(format'dMMMMyyyyHH.mm.ss') // //Unabletoround-trip42of98formatstrings. ImportsSystem.Globalization ModuleExample PublicSubMain() DimcultureAsCultureInfo=CultureInfo.CreateSpecificCulture("it-IT") Dimdate1=NewDateTime(2011,02,01,7,30,45,0) Dimdate2AsDateTime Dimtotal,noRoundTripAsInteger ForEachfmtInculture.DateTimeFormat.GetAllDateTimePatterns() total+=1 IfNotDateTime.TryParse(date1.ToString(fmt),date2) noRoundTrip+=1 Console.WriteLine("Unabletoparse{0:"+fmt+"}(format'{1}')", date1,fmt) EndIf Next Console.WriteLine() Console.WriteLine("Unabletoround-trip{0}of{1}formatstrings.", noRoundTrip,total) EndSub EndModule 'Theexampledisplaysthefollowingoutput: 'UnabletoparseTuesday1February20117.30(format'dddddMMMMyyyyH.mm') 'UnabletoparseTuesday1February201107.30(format'dddddMMMMyyyyHH.mm') 'Unabletoparse1-Feb-117.30(format'd-MMM-yyH.mm') 'Unabletoparse1-Feb-1107.30(format'd-MMM-yyHH.mm') 'Unabletoparse1February20117.30(format'dMMMMyyyyH.mm') 'Unabletoparse1February201107.30(format'dMMMMyyyyHH.mm') 'UnabletoparseTuesday1February20117.30.45(format'dddddMMMMyyyyH.mm.ss') 'UnabletoparseTuesday1February201107.30.45(format'dddddMMMMyyyyHH.mm.ss') 'Unabletoparse1-Feb-117.30.45(format'd-MMM-yyH.mm.ss') 'Unabletoparse1-Feb-1107.30.45(format'd-MMM-yyHH.mm.ss') 'Unabletoparse1February20117.30.45(format'dMMMMyyyyH.mm.ss') 'Unabletoparse1February201107.30.45(format'dMMMMyyyyHH.mm.ss') 'Unabletoparse01/02/20117.30(format'dd/MM/yyyyH.mm') 'Unabletoparse01/02/201107.30(format'dd/MM/yyyyHH.mm') 'Unabletoparse01/Feb/20117.30(format'dd/MMM/yyyyH.mm') 'Unabletoparse01/Feb/201107.30(format'dd/MMM/yyyyHH.mm') 'Unabletoparse01/02/117.30(format'dd/MM/yyH.mm') 'Unabletoparse01/02/1107.30(format'dd/MM/yyHH.mm') 'Unabletoparse01.2.117.30(format'dd.M.yyH.mm') 'Unabletoparse01.2.1107.30(format'dd.M.yyHH.mm') 'Unabletoparse1/2/117.30(format'd/M/yyH.mm') 'Unabletoparse1/2/1107.30(format'd/M/yyHH.mm') 'Unabletoparse2011-02-017.30(format'yyyy-MM-ddH.mm') 'Unabletoparse2011-02-0107.30(format'yyyy-MM-ddHH.mm') 'Unabletoparse01/02/20117.30.45(format'dd/MM/yyyyH.mm.ss') 'Unabletoparse01/02/201107.30.45(format'dd/MM/yyyyHH.mm.ss') 'Unabletoparse01/Feb/20117.30.45(format'dd/MMM/yyyyH.mm.ss') 'Unabletoparse01/Feb/201107.30.45(format'dd/MMM/yyyyHH.mm.ss') 'Unabletoparse01/02/117.30.45(format'dd/MM/yyH.mm.ss') 'Unabletoparse01/02/1107.30.45(format'dd/MM/yyHH.mm.ss') 'Unabletoparse01.2.117.30.45(format'dd.M.yyH.mm.ss') 'Unabletoparse01.2.1107.30.45(format'dd.M.yyHH.mm.ss') 'Unabletoparse1/2/117.30.45(format'd/M/yyH.mm.ss') 'Unabletoparse1/2/1107.30.45(format'd/M/yyHH.mm.ss') 'Unabletoparse2011-02-017.30.45(format'yyyy-MM-ddH.mm.ss') 'Unabletoparse2011-02-0107.30.45(format'yyyy-MM-ddHH.mm.ss') 'UnabletoparseTuesday1February20117.30.45(format'dddddMMMMyyyyH.mm.ss') 'UnabletoparseTuesday1February201107.30.45(format'dddddMMMMyyyyHH.mm.ss') 'Unabletoparse1-Feb-117.30.45(format'd-MMM-yyH.mm.ss') 'Unabletoparse1-Feb-1107.30.45(format'd-MMM-yyHH.mm.ss') 'Unabletoparse1February20117.30.45(format'dMMMMyyyyH.mm.ss') 'Unabletoparse1February201107.30.45(format'dMMMMyyyyHH.mm.ss') ' 'Unabletoround-trip42of98formatstrings. 若要剖析可以用數個預先定義的自訂格式表示之日期和時間的字串表示,請呼叫下列其中一種方法: DateTime.ParseExact(String,String[],IFormatProvider,DateTimeStyles) DateTimeOffset.ParseExact(String,String[],IFormatProvider,DateTimeStyles) DateTime.TryParseExact(String,String[],IFormatProvider,DateTimeStyles,DateTime) DateTimeOffset.TryParseExact(String,String[],IFormatProvider,DateTimeStyles,DateTimeOffset) 方法所傳回之陣列中的自訂格式字串,GetAllDateTimePatterns取決於DateTimeFormatInfo物件提供格式設定資訊之文化特性的目前行事曆。

如果行事曆變更,這個方法所傳回的陣列也會變更。

另請參閱 SetAllDateTimePatterns(String[],Char) 適用於 GetAllDateTimePatterns(Char) 傳回可以使用所指定標準格式字串來格式化日期和時間值的所有模式。

public: cli::array<:string>^GetAllDateTimePatterns(charformat); publicstring[]GetAllDateTimePatterns(charformat); memberthis.GetAllDateTimePatterns:char->string[] PublicFunctionGetAllDateTimePatterns(formatAsChar)AsString() 參數 format Char 標準格式字串。

傳回 String[] 包含標準模式的陣列,其中的日期和時間值可以使用指定的格式字串格式化。

例外狀況 ArgumentException format不是有效的標準格式字串。

範例 下列範例會顯示目前行事曆的日期和時間模式。

#using usingnamespaceSystem; usingnamespaceSystem::Globalization; publicrefclassSamplesDateTimeFormatInfo { public: staticvoidMain() { //CreatesanewDateTimeFormatinfo. DateTimeFormatInfo^myDtfi=gcnewDateTimeFormatInfo; //Getsandprintsallthepatterns array^myPatternsArray=myDtfi->GetAllDateTimePatterns(); Console::WriteLine("ALLthepatterns:"); PrintIndexAndValues(myPatternsArray); //Getsandprintsthepattern(s)associatedwithsomeoftheformatcharacters. myPatternsArray=myDtfi->GetAllDateTimePatterns('d'); Console::WriteLine("Thepatternsfor'd':"); PrintIndexAndValues(myPatternsArray); myPatternsArray=myDtfi->GetAllDateTimePatterns('D'); Console::WriteLine("Thepatternsfor'D':"); PrintIndexAndValues(myPatternsArray); myPatternsArray=myDtfi->GetAllDateTimePatterns('f'); Console::WriteLine("Thepatternsfor'f':"); PrintIndexAndValues(myPatternsArray); myPatternsArray=myDtfi->GetAllDateTimePatterns('F'); Console::WriteLine("Thepatternsfor'F':"); PrintIndexAndValues(myPatternsArray); myPatternsArray=myDtfi->GetAllDateTimePatterns('r'); Console::WriteLine("Thepatternsfor'r':"); PrintIndexAndValues(myPatternsArray); myPatternsArray=myDtfi->GetAllDateTimePatterns('R'); Console::WriteLine("Thepatternsfor'R':"); PrintIndexAndValues(myPatternsArray); } public: staticvoidPrintIndexAndValues(array^myArray){ inti=0; foreach(String^sinmyArray) Console::WriteLine("\t[{0}]:\t{1}",i++,s); Console::WriteLine(); } }; intmain() { SamplesDateTimeFormatInfo::Main(); } /* Thiscodeproducesthefollowingoutput. ALLthepatterns: [0]:MM/dd/yyyy [1]:dddd,ddMMMMyyyy [2]:dddd,ddMMMMyyyyHH:mm [3]:dddd,ddMMMMyyyyhh:mmtt [4]:dddd,ddMMMMyyyyH:mm [5]:dddd,ddMMMMyyyyh:mmtt [6]:dddd,ddMMMMyyyyHH:mm:ss [7]:MM/dd/yyyyHH:mm [8]:MM/dd/yyyyhh:mmtt [9]:MM/dd/yyyyH:mm [10]:MM/dd/yyyyh:mmtt [11]:MM/dd/yyyyHH:mm:ss [12]:MMMMdd [13]:MMMMdd [14]:ddd,ddMMMyyyyHH':'mm':'ss'GMT' [15]:ddd,ddMMMyyyyHH':'mm':'ss'GMT' [16]:yyyy'-'MM'-'dd'T'HH':'mm':'ss [17]:HH:mm [18]:hh:mmtt [19]:H:mm [20]:h:mmtt [21]:HH:mm:ss [22]:yyyy'-'MM'-'ddHH':'mm':'ss'Z' [23]:dddd,ddMMMMyyyyHH:mm:ss [24]:yyyyMMMM [25]:yyyyMMMM Thepatternsfor'd': [0]:MM/dd/yyyy Thepatternsfor'D': [0]:dddd,ddMMMMyyyy Thepatternsfor'f': [0]:dddd,ddMMMMyyyyHH:mm [1]:dddd,ddMMMMyyyyhh:mmtt [2]:dddd,ddMMMMyyyyH:mm [3]:dddd,ddMMMMyyyyh:mmtt Thepatternsfor'F': [0]:dddd,ddMMMMyyyyHH:mm:ss Thepatternsfor'r': [0]:ddd,ddMMMyyyyHH':'mm':'ss'GMT' Thepatternsfor'R': [0]:ddd,ddMMMyyyyHH':'mm':'ss'GMT' */ usingSystem; usingSystem.Globalization; publicclassSamplesDateTimeFormatInfo{ publicstaticvoidMain(){ //CreatesanewDateTimeFormatinfo. DateTimeFormatInfomyDtfi=newDateTimeFormatInfo(); //Getsandprintsallthepatterns. String[]myPatternsArray=myDtfi.GetAllDateTimePatterns(); Console.WriteLine("ALLthepatterns:"); PrintIndexAndValues(myPatternsArray); //Getsandprintsthepattern(s)associatedwithsomeoftheformatcharacters. myPatternsArray=myDtfi.GetAllDateTimePatterns('d'); Console.WriteLine("Thepatternsfor'd':"); PrintIndexAndValues(myPatternsArray); myPatternsArray=myDtfi.GetAllDateTimePatterns('D'); Console.WriteLine("Thepatternsfor'D':"); PrintIndexAndValues(myPatternsArray); myPatternsArray=myDtfi.GetAllDateTimePatterns('f'); Console.WriteLine("Thepatternsfor'f':"); PrintIndexAndValues(myPatternsArray); myPatternsArray=myDtfi.GetAllDateTimePatterns('F'); Console.WriteLine("Thepatternsfor'F':"); PrintIndexAndValues(myPatternsArray); myPatternsArray=myDtfi.GetAllDateTimePatterns('r'); Console.WriteLine("Thepatternsfor'r':"); PrintIndexAndValues(myPatternsArray); myPatternsArray=myDtfi.GetAllDateTimePatterns('R'); Console.WriteLine("Thepatternsfor'R':"); PrintIndexAndValues(myPatternsArray); } publicstaticvoidPrintIndexAndValues(String[]myArray){ inti=0; foreach(stringsinmyArray) Console.WriteLine("\t[{0}]:\t{1}",i++,s); Console.WriteLine(); } } /* Thiscodeproducesthefollowingoutput. ALLthepatterns: [0]:MM/dd/yyyy [1]:dddd,ddMMMMyyyy [2]:dddd,ddMMMMyyyyHH:mm [3]:dddd,ddMMMMyyyyhh:mmtt [4]:dddd,ddMMMMyyyyH:mm [5]:dddd,ddMMMMyyyyh:mmtt [6]:dddd,ddMMMMyyyyHH:mm:ss [7]:MM/dd/yyyyHH:mm [8]:MM/dd/yyyyhh:mmtt [9]:MM/dd/yyyyH:mm [10]:MM/dd/yyyyh:mmtt [11]:MM/dd/yyyyHH:mm:ss [12]:MMMMdd [13]:MMMMdd [14]:ddd,ddMMMyyyyHH':'mm':'ss'GMT' [15]:ddd,ddMMMyyyyHH':'mm':'ss'GMT' [16]:yyyy'-'MM'-'dd'T'HH':'mm':'ss [17]:HH:mm [18]:hh:mmtt [19]:H:mm [20]:h:mmtt [21]:HH:mm:ss [22]:yyyy'-'MM'-'ddHH':'mm':'ss'Z' [23]:dddd,ddMMMMyyyyHH:mm:ss [24]:yyyyMMMM [25]:yyyyMMMM Thepatternsfor'd': [0]:MM/dd/yyyy Thepatternsfor'D': [0]:dddd,ddMMMMyyyy Thepatternsfor'f': [0]:dddd,ddMMMMyyyyHH:mm [1]:dddd,ddMMMMyyyyhh:mmtt [2]:dddd,ddMMMMyyyyH:mm [3]:dddd,ddMMMMyyyyh:mmtt Thepatternsfor'F': [0]:dddd,ddMMMMyyyyHH:mm:ss Thepatternsfor'r': [0]:ddd,ddMMMyyyyHH':'mm':'ss'GMT' Thepatternsfor'R': [0]:ddd,ddMMMyyyyHH':'mm':'ss'GMT' */ ImportsSystem.Globalization PublicClassSamplesDateTimeFormatInfo PublicSharedSubMain() 'CreatesanewDateTimeFormatinfo. DimmyDtfiAsNewDateTimeFormatInfo() 'Getsandprintsallthepatterns. DimmyPatternsArrayAsString()=myDtfi.GetAllDateTimePatterns() Console.WriteLine("ALLthepatterns:") PrintIndexAndValues(myPatternsArray) 'Getsandprintsthepattern(s)associatedwithsomeoftheformatcharacters. myPatternsArray=myDtfi.GetAllDateTimePatterns("d"c) Console.WriteLine("Thepatternsfor'd':") PrintIndexAndValues(myPatternsArray) myPatternsArray=myDtfi.GetAllDateTimePatterns("D"c) Console.WriteLine("Thepatternsfor'D':") PrintIndexAndValues(myPatternsArray) myPatternsArray=myDtfi.GetAllDateTimePatterns("f"c) Console.WriteLine("Thepatternsfor'f':") PrintIndexAndValues(myPatternsArray) myPatternsArray=myDtfi.GetAllDateTimePatterns("F"c) Console.WriteLine("Thepatternsfor'F':") PrintIndexAndValues(myPatternsArray) myPatternsArray=myDtfi.GetAllDateTimePatterns("r"c) Console.WriteLine("Thepatternsfor'r':") PrintIndexAndValues(myPatternsArray) myPatternsArray=myDtfi.GetAllDateTimePatterns("R"c) Console.WriteLine("Thepatternsfor'R':") PrintIndexAndValues(myPatternsArray) EndSub PublicSharedSubPrintIndexAndValues(myArray()AsString) DimiAsInteger=0 DimsAsString ForEachsInmyArray Console.WriteLine(ControlChars.Tab+"[{0}]:"+ControlChars.Tab_ +"{1}",i,s) i+=1 Nexts Console.WriteLine() EndSub EndClass 'Thiscodeproducesthefollowingoutput. ' 'ALLthepatterns: ' [0]: MM/dd/yyyy ' [1]: dddd,ddMMMMyyyy ' [2]: dddd,ddMMMMyyyyHH:mm ' [3]: dddd,ddMMMMyyyyhh:mmtt ' [4]: dddd,ddMMMMyyyyH:mm ' [5]: dddd,ddMMMMyyyyh:mmtt ' [6]: dddd,ddMMMMyyyyHH:mm:ss ' [7]: MM/dd/yyyyHH:mm ' [8]: MM/dd/yyyyhh:mmtt ' [9]: MM/dd/yyyyH:mm ' [10]: MM/dd/yyyyh:mmtt ' [11]: MM/dd/yyyyHH:mm:ss ' [12]: MMMMdd ' [13]: MMMMdd ' [14]: ddd,ddMMMyyyyHH':'mm':'ss'GMT' ' [15]: ddd,ddMMMyyyyHH':'mm':'ss'GMT' ' [16]: yyyy'-'MM'-'dd'T'HH':'mm':'ss ' [17]: HH:mm ' [18]: hh:mmtt ' [19]: H:mm ' [20]: h:mmtt ' [21]: HH:mm:ss ' [22]: yyyy'-'MM'-'ddHH':'mm':'ss'Z' ' [23]: dddd,ddMMMMyyyyHH:mm:ss ' [24]: yyyyMMMM ' [25]: yyyyMMMM ' 'Thepatternsfor'd': ' [0]: MM/dd/yyyy ' 'Thepatternsfor'D': ' [0]: dddd,ddMMMMyyyy ' 'Thepatternsfor'f': ' [0]: dddd,ddMMMMyyyyHH:mm ' [1]: dddd,ddMMMMyyyyhh:mmtt ' [2]: dddd,ddMMMMyyyyH:mm ' [3]: dddd,ddMMMMyyyyh:mmtt ' 'Thepatternsfor'F': ' [0]: dddd,ddMMMMyyyyHH:mm:ss ' 'Thepatternsfor'r': ' [0]: ddd,ddMMMyyyyHH':'mm':'ss'GMT' ' 'Thepatternsfor'R': ' [0]: ddd,ddMMMyyyyHH':'mm':'ss'GMT' ' 備註 這個方法會傳回陣列,其中包含對應至特定標準格式字串的自訂格式字串。

請參閱標準日期和時間格式字串,以取得標準格式字串的清單。

您可以GetAllDateTimePatterns在格式化作業中,于方法所傳回的陣列中使用自訂格式字串。

但是,如果您這樣做,則在該格式化作業中傳回之日期和時間值的字串表示,無法一律由Parse和方法成功剖析TryParse。

因此,您無法假設方法所傳回的自訂格式字串GetAllDateTimePatterns可以用來來回行程日期和時間值。

下列範例說明此問題。

它會抓取DateTimeFormatInfo包含俄羅斯(俄文)文化特性之格式資訊的物件。

它會GetAllDateTimePatterns(Char)針對每個標準格式字串呼叫方法,然後將傳回陣列中的每個自訂格式字串傳遞給DateTime.ToString(String)方法,以建立日期和時間的字串表示。

然後,此範例會藉由呼叫方法,嘗試剖析此值DateTime.TryParse(String,DateTime)。

如範例的輸出所示,某些自訂格式字串不會產生成功往返的日期和時間值。

usingSystem; usingSystem.Globalization; publicclassExample { publicstaticvoidMain() { CultureInfoculture=CultureInfo.CreateSpecificCulture("ru-RU"); char[]formats={'d','D','f','F','g','G','m','o', 'r','s','t','T','u','U','y'}; DateTimedate1=newDateTime(2011,02,01,7,30,45,0); DateTimedate2; inttotal=0; intnoRoundTrip=0; foreach(varfmtinformats){ total=0; noRoundTrip=0; foreach(varpatterninculture.DateTimeFormat.GetAllDateTimePatterns(fmt)){ total++; if(!DateTime.TryParse(date1.ToString(pattern),outdate2)){ noRoundTrip++; Console.WriteLine("Unabletoparse{0:"+pattern+"}(format'{1}')", date1,pattern); } } if(noRoundTrip>0) Console.WriteLine("{0}:Unabletoround-trip{1}of{2}formatstrings.\n", fmt,noRoundTrip,total); else Console.WriteLine("{0}:Allcustomformatstringsroundtrip.\n",fmt); } } } //Theexampledisplaysthefollowingoutput: //d:Allcustomformatstringsroundtrip. // //Unabletoparse1February2011г.(format'dMMMMyyyy'г.'') //Unabletoparse01February2011г.(format'ddMMMMyyyy'г.'') //D:Unabletoround-trip2of2formatstrings. // //Unabletoparse1February2011г.7:30(format'dMMMMyyyy'г.'H:mm') //Unabletoparse1February2011г.07:30(format'dMMMMyyyy'г.'HH:mm') //Unabletoparse01February2011г.7:30(format'ddMMMMyyyy'г.'H:mm') //Unabletoparse01February2011г.07:30(format'ddMMMMyyyy'г.'HH:mm') //f:Unabletoround-trip4of4formatstrings. // //Unabletoparse1February2011г.7:30:45(format'dMMMMyyyy'г.'H:mm:ss') //Unabletoparse1February2011г.07:30:45(format'dMMMMyyyy'г.'HH:mm:ss') //Unabletoparse01February2011г.7:30:45(format'ddMMMMyyyy'г.'H:mm:ss') //Unabletoparse01February2011г.07:30:45(format'ddMMMMyyyy'г.'HH:mm:ss') //F:Unabletoround-trip4of4formatstrings. // //g:Allcustomformatstringsroundtrip. // //G:Allcustomformatstringsroundtrip. // //m:Allcustomformatstringsroundtrip. // //o:Allcustomformatstringsroundtrip. // //r:Allcustomformatstringsroundtrip. // //s:Allcustomformatstringsroundtrip. // //t:Allcustomformatstringsroundtrip. // //T:Allcustomformatstringsroundtrip. // //u:Allcustomformatstringsroundtrip. // //Unabletoparse1February2011г.7:30:45(format'dMMMMyyyy'г.'H:mm:ss') //Unabletoparse1February2011г.07:30:45(format'dMMMMyyyy'г.'HH:mm:ss') //Unabletoparse01February2011г.7:30:45(format'ddMMMMyyyy'г.'H:mm:ss') //Unabletoparse01February2011г.07:30:45(format'ddMMMMyyyy'г.'HH:mm:ss') //U:Unabletoround-trip4of4formatstrings. // //y:Allcustomformatstringsroundtrip. ImportsSystem.Globalization ModuleExample PublicSubMain() DimcultureAsCultureInfo=CultureInfo.CreateSpecificCulture("ru-RU") Dimformats()AsString={"d","D","f","F","g","G","m", "o","r","s","t","T","u","U","y"} Dimdate1=NewDateTime(2011,02,01,7,30,45,0) Dimdate2AsDateTime Dimtotal,noRoundTripAsInteger ForEachfmtInformats total=0:noRoundTrip=0 ForEachpatternInculture.DateTimeFormat.GetAllDateTimePatterns(CChar(fmt)) total+=1 IfNotDateTime.TryParse(date1.ToString(pattern),date2) noRoundTrip+=1 Console.WriteLine("Unabletoparse{0:"+pattern+"}(format'{1}')", date1,pattern) EndIf Next IfnoRoundTrip>0Then Console.WriteLine("{0}:Unabletoround-trip{1}of{2}formatstrings.", fmt,noRoundTrip,total) Console.WriteLine() Else Console.WriteLine("{0}:Allcustomformatstringsroundtrip.",fmt) Console.WriteLine() EndIf Next EndSub EndModule 'Theexampledisplaysthefollowingoutput: 'd:Allcustomformatstringsroundtrip. ' 'Unabletoparse1February2011г.(format'dMMMMyyyy'г.'') 'Unabletoparse01February2011г.(format'ddMMMMyyyy'г.'') 'D:Unabletoround-trip2of2formatstrings. ' 'Unabletoparse1February2011г.7:30(format'dMMMMyyyy'г.'H:mm') 'Unabletoparse1February2011г.07:30(format'dMMMMyyyy'г.'HH:mm') 'Unabletoparse01February2011г.7:30(format'ddMMMMyyyy'г.'H:mm') 'Unabletoparse01February2011г.07:30(format'ddMMMMyyyy'г.'HH:mm') 'f:Unabletoround-trip4of4formatstrings. ' 'Unabletoparse1February2011г.7:30:45(format'dMMMMyyyy'г.'H:mm:ss') 'Unabletoparse1February2011г.07:30:45(format'dMMMMyyyy'г.'HH:mm:ss') 'Unabletoparse01February2011г.7:30:45(format'ddMMMMyyyy'г.'H:mm:ss') 'Unabletoparse01February2011г.07:30:45(format'ddMMMMyyyy'г.'HH:mm:ss') 'F:Unabletoround-trip4of4formatstrings. ' 'g:Allcustomformatstringsroundtrip. ' 'G:Allcustomformatstringsroundtrip. ' 'm:Allcustomformatstringsroundtrip. ' 'o:Allcustomformatstringsroundtrip. ' 'r:Allcustomformatstringsroundtrip. ' 's:Allcustomformatstringsroundtrip. ' 't:Allcustomformatstringsroundtrip. ' 'T:Allcustomformatstringsroundtrip. ' 'u:Allcustomformatstringsroundtrip. ' 'Unabletoparse1February2011г.7:30:45(format'dMMMMyyyy'г.'H:mm:ss') 'Unabletoparse1February2011г.07:30:45(format'dMMMMyyyy'г.'HH:mm:ss') 'Unabletoparse01February2011г.7:30:45(format'ddMMMMyyyy'г.'H:mm:ss') 'Unabletoparse01February2011г.07:30:45(format'ddMMMMyyyy'г.'HH:mm:ss') 'U:Unabletoround-trip4of4formatstrings. ' 'y:Allcustomformatstringsroundtrip. 若要剖析可以用數個預先定義的自訂格式表示之日期和時間的字串表示,請呼叫下列其中一種方法: DateTime.ParseExact(String,String[],IFormatProvider,DateTimeStyles) DateTimeOffset.ParseExact(String,String[],IFormatProvider,DateTimeStyles) DateTime.TryParseExact(String,String[],IFormatProvider,DateTimeStyles,DateTime) DateTimeOffset.TryParseExact(String,String[],IFormatProvider,DateTimeStyles,DateTimeOffset) 方法所傳回之陣列中的自訂格式字串,GetAllDateTimePatterns取決於DateTimeFormatInfo物件提供格式設定資訊之文化特性的目前行事曆。

如果行事曆變更,這個方法所傳回的陣列也會變更。

另請參閱 SetAllDateTimePatterns(String[],Char) 適用於 本文內容



請為這篇文章評分?