DateTimeFormatInfo.GetAllDateTimePatterns 方法 - Microsoft ...
文章推薦指數: 80 %
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
請參閱標準日期和時間格式字串,以取得標準格式字串的清單。
您可以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)
適用於
本文內容
延伸文章資訊
- 1Checking Date format from a string in C# - Stack Overflow
- 2String.Format yyyy/MM/dd? 誤會大了 - 黑暗執行緒
換句話說,如果日期格式被設定為yyyy-MM-dd,則DateTimeFormatInfo.DataSeparator = "-",此時string.Format("{0:yyyy/MM/dd}...
- 3DateTime.ToString 方法(System) | Microsoft Docs
目前DateTime 物件值的字串表示,如 format 及 provider 所指定。 ... Aug 17 '00 * dddd, MMMM dd :Thursday, August 17 ...
- 4c# date format yyyy-mm-dd Code Example
DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")
- 5C# DateTime Format:Working with Date &Time format in C#|
You can convert the dateTime to any DateTime format by simply adding the required format in ToStr...