DateTime.ToString 方法(System) | Microsoft Docs

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

目前DateTime 物件值的字串表示,如 format 及 provider 所指定。

... Aug 17 '00 * dddd, MMMM dd :Thursday, August 17 * M/yy :8/00 * dd-MM-yy :17-08-00 */ } } 跳到主要內容 已不再支援此瀏覽器。

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

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

隱私權原則。

送出 謝謝。

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

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

將目前DateTime物件的值,轉換為其相等的字串表示。

本文內容 多載 ToString(String,IFormatProvider) 使用指定的格式和特定文化特性的格式資訊,將目前DateTime物件的值,轉換為其相等的字串表示。

ToString(String) 使用指定的格式和目前文化特性的格式化慣例,將目前DateTime物件的值轉換為其相等的字串表示。

ToString(IFormatProvider) 使用指定的特定文化特性格式資訊,將目前DateTime物件的值轉換為其相等的字串表示。

ToString() 使用目前文化特性的格式化慣例,將目前DateTime物件的值轉換為其相等的字串表示。

ToString(String,IFormatProvider) 使用指定的格式和特定文化特性的格式資訊,將目前DateTime物件的值,轉換為其相等的字串表示。

public: virtualSystem::String^ToString(System::String^format,IFormatProvider^provider); publicstringToString(stringformat,IFormatProviderprovider); publicstringToString(string?format,IFormatProvider?provider); overridethis.ToString:string*IFormatProvider->string PublicFunctionToString(formatAsString,providerAsIFormatProvider)AsString 參數 format String 標準或自訂的日期和時間格式字串。

provider IFormatProvider 物件,提供特定文化特性格式資訊。

傳回 String 目前DateTime物件值的字串表示,如format及provider所指定。

實作 ToString(String,IFormatProvider) 例外狀況 FormatException format長度為1,而且不屬於針對DateTimeFormatInfo定義的格式規範字元。

-或- format不包含有效的自訂格式模式。

ArgumentOutOfRangeException 日期和時間超出provider使用之曆法所支援的日期範圍。

範例 下列範例會使用每個標準日期時間格式字串來顯示四種不同文化特性之日期和時間的字串表示。

usingSystem; usingSystem.Globalization; publicclassToStringExample4 { publicstaticvoidMain1() { //Createanarrayofallsupportedstandarddateandtimeformatspecifiers. string[]formats={"d","D","f","F","g","G","m","o","r", "s","t","T","u","U","Y"}; //Createanarrayoffourcultures. CultureInfo[]cultures={CultureInfo.GetCultureInfo("de-DE"), CultureInfo.GetCultureInfo("en-US"), CultureInfo.GetCultureInfo("es-ES"), CultureInfo.GetCultureInfo("fr-FR")}; //Definedatetobedisplayed. DateTimedateToDisplay=newDateTime(2008,10,31,17,4,32); //Iterateeachstandardformatspecifier. foreach(stringformatSpecifierinformats) { foreach(CultureInfocultureincultures) Console.WriteLine("{0}FormatSpecifier{1,10}Culture{2,40}", formatSpecifier,culture.Name, dateToDisplay.ToString(formatSpecifier,culture)); Console.WriteLine(); } } } //Theexampledisplaysthefollowingoutput: //dFormatSpecifierde-DECulture31.10.2008 //dFormatSpecifieren-USCulture10/31/2008 //dFormatSpecifieres-ESCulture31/10/2008 //dFormatSpecifierfr-FRCulture31/10/2008 // //DFormatSpecifierde-DECultureFreitag,31.Oktober2008 //DFormatSpecifieren-USCultureFriday,October31,2008 //DFormatSpecifieres-ESCultureviernes,31deoctubrede2008 //DFormatSpecifierfr-FRCulturevendredi31octobre2008 // //fFormatSpecifierde-DECultureFreitag,31.Oktober200817:04 //fFormatSpecifieren-USCultureFriday,October31,20085:04PM //fFormatSpecifieres-ESCultureviernes,31deoctubrede200817:04 //fFormatSpecifierfr-FRCulturevendredi31octobre200817:04 // //FFormatSpecifierde-DECultureFreitag,31.Oktober200817:04:32 //FFormatSpecifieren-USCultureFriday,October31,20085:04:32PM //FFormatSpecifieres-ESCultureviernes,31deoctubrede200817:04:32 //FFormatSpecifierfr-FRCulturevendredi31octobre200817:04:32 // //gFormatSpecifierde-DECulture31.10.200817:04 //gFormatSpecifieren-USCulture10/31/20085:04PM //gFormatSpecifieres-ESCulture31/10/200817:04 //gFormatSpecifierfr-FRCulture31/10/200817:04 // //GFormatSpecifierde-DECulture31.10.200817:04:32 //GFormatSpecifieren-USCulture10/31/20085:04:32PM //GFormatSpecifieres-ESCulture31/10/200817:04:32 //GFormatSpecifierfr-FRCulture31/10/200817:04:32 // //mFormatSpecifierde-DECulture31.Oktober //mFormatSpecifieren-USCultureOctober31 //mFormatSpecifieres-ESCulture31deoctubre //mFormatSpecifierfr-FRCulture31octobre // //oFormatSpecifierde-DECulture2008-10-31T17:04:32.0000000 //oFormatSpecifieren-USCulture2008-10-31T17:04:32.0000000 //oFormatSpecifieres-ESCulture2008-10-31T17:04:32.0000000 //oFormatSpecifierfr-FRCulture2008-10-31T17:04:32.0000000 // //rFormatSpecifierde-DECultureFri,31Oct200817:04:32GMT //rFormatSpecifieren-USCultureFri,31Oct200817:04:32GMT //rFormatSpecifieres-ESCultureFri,31Oct200817:04:32GMT //rFormatSpecifierfr-FRCultureFri,31Oct200817:04:32GMT // //sFormatSpecifierde-DECulture2008-10-31T17:04:32 //sFormatSpecifieren-USCulture2008-10-31T17:04:32 //sFormatSpecifieres-ESCulture2008-10-31T17:04:32 //sFormatSpecifierfr-FRCulture2008-10-31T17:04:32 // //tFormatSpecifierde-DECulture17:04 //tFormatSpecifieren-USCulture5:04PM //tFormatSpecifieres-ESCulture17:04 //tFormatSpecifierfr-FRCulture17:04 // //TFormatSpecifierde-DECulture17:04:32 //TFormatSpecifieren-USCulture5:04:32PM //TFormatSpecifieres-ESCulture17:04:32 //TFormatSpecifierfr-FRCulture17:04:32 // //uFormatSpecifierde-DECulture2008-10-3117:04:32Z //uFormatSpecifieren-USCulture2008-10-3117:04:32Z //uFormatSpecifieres-ESCulture2008-10-3117:04:32Z //uFormatSpecifierfr-FRCulture2008-10-3117:04:32Z // //UFormatSpecifierde-DECultureFreitag,31.Oktober200809:04:32 //UFormatSpecifieren-USCultureFriday,October31,20089:04:32AM //UFormatSpecifieres-ESCultureviernes,31deoctubrede20089:04:32 //UFormatSpecifierfr-FRCulturevendredi31octobre200809:04:32 // //YFormatSpecifierde-DECultureOktober2008 //YFormatSpecifieren-USCultureOctober2008 //YFormatSpecifieres-ESCultureoctubrede2008 //YFormatSpecifierfr-FRCultureoctobre2008 openSystem openSystem.Globalization //Createalistofallsupportedstandarddateandtimeformatspecifiers. letformats= ["d";"D";"f";"F";"g";"G";"m";"o" "r";"s";"t";"T";"u";"U";"Y"] //Createalistoffourcultures. letcultures= [CultureInfo.GetCultureInfo"de-DE" CultureInfo.GetCultureInfo"en-US" CultureInfo.GetCultureInfo"es-ES" CultureInfo.GetCultureInfo"fr-FR"] //Definedatetobedisplayed. letdateToDisplay=DateTime(2008,10,31,17,4,32) //Iterateeachstandardformatspecifier. forformatSpecifierinformatsdo forcultureinculturesdo printfn$"{formatSpecifier}FormatSpecifier{culture.Name,10}Culture{dateToDisplay.ToString(formatSpecifier,culture),40}" printfn"" //Theexampledisplaysthefollowingoutput: //dFormatSpecifierde-DECulture31.10.2008 //dFormatSpecifieren-USCulture10/31/2008 //dFormatSpecifieres-ESCulture31/10/2008 //dFormatSpecifierfr-FRCulture31/10/2008 // //DFormatSpecifierde-DECultureFreitag,31.Oktober2008 //DFormatSpecifieren-USCultureFriday,October31,2008 //DFormatSpecifieres-ESCultureviernes,31deoctubrede2008 //DFormatSpecifierfr-FRCulturevendredi31octobre2008 // //fFormatSpecifierde-DECultureFreitag,31.Oktober200817:04 //fFormatSpecifieren-USCultureFriday,October31,20085:04PM //fFormatSpecifieres-ESCultureviernes,31deoctubrede200817:04 //fFormatSpecifierfr-FRCulturevendredi31octobre200817:04 // //FFormatSpecifierde-DECultureFreitag,31.Oktober200817:04:32 //FFormatSpecifieren-USCultureFriday,October31,20085:04:32PM //FFormatSpecifieres-ESCultureviernes,31deoctubrede200817:04:32 //FFormatSpecifierfr-FRCulturevendredi31octobre200817:04:32 // //gFormatSpecifierde-DECulture31.10.200817:04 //gFormatSpecifieren-USCulture10/31/20085:04PM //gFormatSpecifieres-ESCulture31/10/200817:04 //gFormatSpecifierfr-FRCulture31/10/200817:04 // //GFormatSpecifierde-DECulture31.10.200817:04:32 //GFormatSpecifieren-USCulture10/31/20085:04:32PM //GFormatSpecifieres-ESCulture31/10/200817:04:32 //GFormatSpecifierfr-FRCulture31/10/200817:04:32 // //mFormatSpecifierde-DECulture31.Oktober //mFormatSpecifieren-USCultureOctober31 //mFormatSpecifieres-ESCulture31deoctubre //mFormatSpecifierfr-FRCulture31octobre // //oFormatSpecifierde-DECulture2008-10-31T17:04:32.0000000 //oFormatSpecifieren-USCulture2008-10-31T17:04:32.0000000 //oFormatSpecifieres-ESCulture2008-10-31T17:04:32.0000000 //oFormatSpecifierfr-FRCulture2008-10-31T17:04:32.0000000 // //rFormatSpecifierde-DECultureFri,31Oct200817:04:32GMT //rFormatSpecifieren-USCultureFri,31Oct200817:04:32GMT //rFormatSpecifieres-ESCultureFri,31Oct200817:04:32GMT //rFormatSpecifierfr-FRCultureFri,31Oct200817:04:32GMT // //sFormatSpecifierde-DECulture2008-10-31T17:04:32 //sFormatSpecifieren-USCulture2008-10-31T17:04:32 //sFormatSpecifieres-ESCulture2008-10-31T17:04:32 //sFormatSpecifierfr-FRCulture2008-10-31T17:04:32 // //tFormatSpecifierde-DECulture17:04 //tFormatSpecifieren-USCulture5:04PM //tFormatSpecifieres-ESCulture17:04 //tFormatSpecifierfr-FRCulture17:04 // //TFormatSpecifierde-DECulture17:04:32 //TFormatSpecifieren-USCulture5:04:32PM //TFormatSpecifieres-ESCulture17:04:32 //TFormatSpecifierfr-FRCulture17:04:32 // //uFormatSpecifierde-DECulture2008-10-3117:04:32Z //uFormatSpecifieren-USCulture2008-10-3117:04:32Z //uFormatSpecifieres-ESCulture2008-10-3117:04:32Z //uFormatSpecifierfr-FRCulture2008-10-3117:04:32Z // //UFormatSpecifierde-DECultureFreitag,31.Oktober200809:04:32 //UFormatSpecifieren-USCultureFriday,October31,20089:04:32AM //UFormatSpecifieres-ESCultureviernes,31deoctubrede20089:04:32 //UFormatSpecifierfr-FRCulturevendredi31octobre200809:04:32 // //YFormatSpecifierde-DECultureOktober2008 //YFormatSpecifieren-USCultureOctober2008 //YFormatSpecifieres-ESCultureoctubrede2008 //YFormatSpecifierfr-FRCultureoctobre2008 ImportsSystem.Globalization ModuleExample PublicSubMain4() 'Createanarrayofallsupportedstandarddateandtimeformatspecifiers. Dimformats()AsString={"d","D","f","F","g","G","m","o","r", "s","t","T","u","U","Y"} 'Createanarrayoffourcultures. Dimcultures()AsCultureInfo={CultureInfo.GetCultureInfo("de-DE"), CultureInfo.GetCultureInfo("en-US"), CultureInfo.GetCultureInfo("es-ES"), CultureInfo.GetCultureInfo("fr-FR")} 'Definedatetobedisplayed. DimdateToDisplayAsDate=#10/1/200817:04:32# 'Iterateeachstandardformatspecifier. ForEachformatSpecifierAsStringInformats ForEachcultureAsCultureInfoIncultures Console.WriteLine("{0}FormatSpecifier{1,10}Culture{2,40}", formatSpecifier,culture.Name, dateToDisplay.ToString(formatSpecifier,culture)) Next Console.WriteLine() Next EndSub EndModule 'Theexampledisplaysthefollowingoutput: 'dFormatSpecifierde-DECulture01.10.2008 'dFormatSpecifieren-USCulture10/1/2008 'dFormatSpecifieres-ESCulture01/10/2008 'dFormatSpecifierfr-FRCulture01/10/2008 ' 'DFormatSpecifierde-DECultureMittwoch,1.Oktober2008 'DFormatSpecifieren-USCultureWednesday,October01,2008 'DFormatSpecifieres-ESCulturemiércoles,01deoctubrede2008 'DFormatSpecifierfr-FRCulturemercredi1octobre2008 ' 'fFormatSpecifierde-DECultureMittwoch,1.Oktober200817:04 'fFormatSpecifieren-USCultureWednesday,October01,20085:04PM 'fFormatSpecifieres-ESCulturemiércoles,01deoctubrede200817:04 'fFormatSpecifierfr-FRCulturemercredi1octobre200817:04 ' 'FFormatSpecifierde-DECultureMittwoch,1.Oktober200817:04:32 'FFormatSpecifieren-USCultureWednesday,October01,20085:04:32PM 'FFormatSpecifieres-ESCulturemiércoles,01deoctubrede200817:04:3 'FFormatSpecifierfr-FRCulturemercredi1octobre200817:04:32 ' 'gFormatSpecifierde-DECulture01.10.200817:04 'gFormatSpecifieren-USCulture10/1/20085:04PM 'gFormatSpecifieres-ESCulture01/10/200817:04 'gFormatSpecifierfr-FRCulture01/10/200817:04 ' 'GFormatSpecifierde-DECulture01.10.200817:04:32 'GFormatSpecifieren-USCulture10/1/20085:04:32PM 'GFormatSpecifieres-ESCulture01/10/200817:04:32 'GFormatSpecifierfr-FRCulture01/10/200817:04:32 ' 'mFormatSpecifierde-DECulture01Oktober 'mFormatSpecifieren-USCultureOctober01 'mFormatSpecifieres-ESCulture01octubre 'mFormatSpecifierfr-FRCulture1octobre ' 'oFormatSpecifierde-DECulture2008-10-01T17:04:32.0000000 'oFormatSpecifieren-USCulture2008-10-01T17:04:32.0000000 'oFormatSpecifieres-ESCulture2008-10-01T17:04:32.0000000 'oFormatSpecifierfr-FRCulture2008-10-01T17:04:32.0000000 ' 'rFormatSpecifierde-DECultureWed,01Oct200817:04:32GMT 'rFormatSpecifieren-USCultureWed,01Oct200817:04:32GMT 'rFormatSpecifieres-ESCultureWed,01Oct200817:04:32GMT 'rFormatSpecifierfr-FRCultureWed,01Oct200817:04:32GMT ' 'sFormatSpecifierde-DECulture2008-10-01T17:04:32 'sFormatSpecifieren-USCulture2008-10-01T17:04:32 'sFormatSpecifieres-ESCulture2008-10-01T17:04:32 'sFormatSpecifierfr-FRCulture2008-10-01T17:04:32 ' 'tFormatSpecifierde-DECulture17:04 'tFormatSpecifieren-USCulture5:04PM 'tFormatSpecifieres-ESCulture17:04 'tFormatSpecifierfr-FRCulture17:04 ' 'TFormatSpecifierde-DECulture17:04:32 'TFormatSpecifieren-USCulture5:04:32PM 'TFormatSpecifieres-ESCulture17:04:32 'TFormatSpecifierfr-FRCulture17:04:32 ' 'uFormatSpecifierde-DECulture2008-10-0117:04:32Z 'uFormatSpecifieren-USCulture2008-10-0117:04:32Z 'uFormatSpecifieres-ESCulture2008-10-0117:04:32Z 'uFormatSpecifierfr-FRCulture2008-10-0117:04:32Z ' 'UFormatSpecifierde-DECultureDonnerstag,2.Oktober200800:04:32 'UFormatSpecifieren-USCultureThursday,October02,200812:04:32AM 'UFormatSpecifieres-ESCulturejueves,02deoctubrede20080:04:32 'UFormatSpecifierfr-FRCulturejeudi2octobre200800:04:32 ' 'YFormatSpecifierde-DECultureOktober2008 'YFormatSpecifieren-USCultureOctober,2008 'YFormatSpecifieres-ESCultureoctubrede2008 'YFormatSpecifierfr-FRCultureoctobre2008 下列範例示範使用非變異來將值格式化的不同方式DateTimeDateTimeFormatInfo。

usingnamespaceSystem; usingnamespaceSystem::Globalization; voidmain() { DateTimedt=DateTime::Now; array^format={L"d",L"D",L"f",L"F",L"g",L"G",L"m",L"r",L"s",L"t",L"T",L"u",L"U",L"y",L"dddd,MMMMddyyyy",L"ddd,MMMd\"'\"yy",L"dddd,MMMMdd",L"M/yy",L"dd-MM-yy"}; String^date; for(inti=0;iLength;i++) { date=dt.ToString(format[i],DateTimeFormatInfo::InvariantInfo); Console::WriteLine(String::Concat(format[i],L":",date)); } /**Output. * *d:08/17/2000 *D:Thursday,August17,2000 *f:Thursday,August17,200016:32 *F:Thursday,August17,200016:32:32 *g:08/17/200016:32 *G:08/17/200016:32:32 *m:August17 *r:Thu,17Aug200023:32:32GMT *s:2000-08-17T16:32:32 *t:16:32 *T:16:32:32 *u:2000-08-1723:32:32Z *U:Thursday,August17,200023:32:32 *y:August,2000 *dddd,MMMMddyyyy:Thursday,August172000 *ddd,MMMd"'"yy:Thu,Aug17'00 *dddd,MMMMdd:Thursday,August17 *M/yy:8/00 *dd-MM-yy:17-08-00 */ } openSystem openSystem.Globalization letdt=DateTime.Now letformats= ["d";"D" "f";"F" "g";"G" "m" "r" "s" "t";"T" "u";"U" "y" "dddd,MMMMddyyyy" "ddd,MMMd\"'\"yy" "dddd,MMMMdd" "M/yy" "dd-MM-yy"] forformatinformatsdo letdate=dt.ToString(format,DateTimeFormatInfo.InvariantInfo) printfn$"{format}:{date}" //Output. //d:08/17/2000 //D:Thursday,August17,2000 //f:Thursday,August17,200016:32 //F:Thursday,August17,200016:32:32 //g:08/17/200016:32 //G:08/17/200016:32:32 //m:August17 //r:Thu,17Aug200023:32:32GMT //s:2000-08-17T16:32:32 //t:16:32 //T:16:32:32 //u:2000-08-1723:32:32Z //U:Thursday,August17,200023:32:32 //y:August,2000 //dddd,MMMMddyyyy:Thursday,August172000 //ddd,MMMd"'"yy:Thu,Aug17'00 //dddd,MMMMdd:Thursday,August17 //M/yy:8/00 //dd-MM-yy:17-08-00 usingSystem; usingSystem.Globalization; publicclassMainClass { publicstaticvoidMain(string[]args) { DateTimedt=DateTime.Now; String[]format={ "d","D", "f","F", "g","G", "m", "r", "s", "t","T", "u","U", "y", "dddd,MMMMddyyyy", "ddd,MMMd\"'\"yy", "dddd,MMMMdd", "M/yy", "dd-MM-yy", }; stringdate; for(inti=0;istring PublicFunctionToString(formatAsString)AsString 參數 format String 標準或自訂的日期和時間格式字串。

傳回 String 目前DateTime物件值的字串表示,如format所指定。

例外狀況 FormatException format長度為1,而且不屬於針對DateTimeFormatInfo定義的格式規範字元。

-或- format不包含有效的自訂格式模式。

ArgumentOutOfRangeException 日期和時間超出目前文化特性使用之曆法所支援的日期範圍。

範例 下列範例會使用每個標準日期和時間格式字串,以及選取自訂日期和時間格式字串來顯示值的字串表示DateTime。

此範例的執行緒目前文化特性為en-us。

usingSystem; publicclassDateToStringExample2 { publicstaticvoidMain0() { DateTimedateValue=newDateTime(2008,6,15,21,15,07); //Createanarrayofstandardformatstrings. string[]standardFmts={"d","D","f","F","g","G","m","o", "R","s","t","T","u","U","y"}; //Outputdateandtimeusingeachstandardformatstring. foreach(stringstandardFmtinstandardFmts) Console.WriteLine("{0}:{1}",standardFmt, dateValue.ToString(standardFmt)); Console.WriteLine(); //Createanarrayofsomecustomformatstrings. string[]customFmts={"h:mm:ss.fft","dMMMyyyy","HH:mm:ss.f", "ddMMMHH:mm:ss",@"\Mon\t\h\:M","HH:mm:ss.ffffzzz"}; //Outputdateandtimeusingeachcustomformatstring. foreach(stringcustomFmtincustomFmts) Console.WriteLine("'{0}':{1}",customFmt, dateValue.ToString(customFmt)); } } //Thisexampledisplaysthefollowingoutputtotheconsole: //d:6/15/2008 //D:Sunday,June15,2008 //f:Sunday,June15,20089:15PM //F:Sunday,June15,20089:15:07PM //g:6/15/20089:15PM //G:6/15/20089:15:07PM //m:June15 //o:2008-06-15T21:15:07.0000000 //R:Sun,15Jun200821:15:07GMT //s:2008-06-15T21:15:07 //t:9:15PM //T:9:15:07PM //u:2008-06-1521:15:07Z //U:Monday,June16,20084:15:07AM //y:June,2008 // //'h:mm:ss.fft':9:15:07.00P //'dMMMyyyy':15Jun2008 //'HH:mm:ss.f':21:15:07.0 //'ddMMMHH:mm:ss':15Jun21:15:07 //'\Mon\t\h\:M':Month:6 //'HH:mm:ss.ffffzzz':21:15:07.0000-07:00 openSystem letdateValue=DateTime(2008,6,15,21,15,07) //Createalistofstandardformatstrings. letstandardFmts= ["d";"D";"f";"F";"g";"G";"m";"o" "R";"s";"t";"T";"u";"U";"y"] //Outputdateandtimeusingeachstandardformatstring. forstandardFmtinstandardFmtsdo printfn$"{standardFmt}:{dateValue.ToStringstandardFmt}" printfn"" //Createalistofsomecustomformatstrings. letcustomFmts= ["h:mm:ss.fft";"dMMMyyyy";"HH:mm:ss.f";"ddMMMHH:mm:ss";@"\Mon\t\h\:M";"HH:mm:ss.ffffzzz"] //Outputdateandtimeusingeachcustomformatstring. forcustomFmtincustomFmtsdo printfn$"'{customFmt}':{dateValue.ToStringcustomFmt}" //Thisexampledisplaysthefollowingoutputtotheconsole: //d:6/15/2008 //D:Sunday,June15,2008 //f:Sunday,June15,20089:15PM //F:Sunday,June15,20089:15:07PM //g:6/15/20089:15PM //G:6/15/20089:15:07PM //m:June15 //o:2008-06-15T21:15:07.0000000 //R:Sun,15Jun200821:15:07GMT //s:2008-06-15T21:15:07 //t:9:15PM //T:9:15:07PM //u:2008-06-1521:15:07Z //U:Monday,June16,20084:15:07AM //y:June,2008 // //'h:mm:ss.fft':9:15:07.00P //'dMMMyyyy':15Jun2008 //'HH:mm:ss.f':21:15:07.0 //'ddMMMHH:mm:ss':15Jun21:15:07 //'\Mon\t\h\:M':Month:6 //'HH:mm:ss.ffffzzz':21:15:07.0000-07:00 ModuleDateToStringExample2 PublicSubMain2() DimdateValueAsDate=#6/15/20089:15:07PM# 'Createanarrayofstandardformatstrings. DimstandardFmts()AsString={"d","D","f","F","g","G", "m","o","R","s","t","T", "u","U","y"} 'Outputdateandtimeusingeachstandardformatstring. ForEachstandardFmtAsStringInstandardFmts Console.WriteLine("{0}:{1}",standardFmt, dateValue.ToString(standardFmt)) Next Console.WriteLine() 'Createanarrayofsomecustomformatstrings. DimcustomFmts()AsString={"h:mm:ss.fft","dMMMyyyy","HH:mm:ss.f", "ddMMMHH:mm:ss","\Mon\t\h\:M","HH:mm:ss.ffffzzz"} 'Outputdateandtimeusingeachcustomformatstring. ForEachcustomFmtAsStringIncustomFmts Console.WriteLine("'{0}':{1}",customFmt, dateValue.ToString(customFmt)) Next EndSub EndModule 'Thisexampledisplaysthefollowingoutputtotheconsole: 'd:6/15/2008 'D:Sunday,June15,2008 'f:Sunday,June15,20089:15PM 'F:Sunday,June15,20089:15:07PM 'g:6/15/20089:15PM 'G:6/15/20089:15:07PM 'm:June15 'o:2008-06-15T21:15:07.0000000 'R:Sun,15Jun200821:15:07GMT 's:2008-06-15T21:15:07 't:9:15PM 'T:9:15:07PM 'u:2008-06-1521:15:07Z 'U:Monday,June16,20084:15:07AM 'y:June,2008 ' ''h:mm:ss.fft':9:15:07.00P ''dMMMyyyy':15Jun2008 ''HH:mm:ss.f':21:15:07.0 ''ddMMMHH:mm:ss':15Jun21:15:07 ''\Mon\t\h\:M':Month:6 ''HH:mm:ss.ffffzzz':21:15:07.0000-07:00 備註 ToString(String)方法會以特定格式傳回日期和時間值的字串標記法,此格式會使用目前文化特性的格式化慣例。

如需詳細資訊,請參閱CultureInfo.CurrentCulture。

format參數應該包含單一格式規範字符(請參閱標準日期和時間格式字串)或自訂格式模式(查看定義傳回字串格式)的自訂日期和時間格式字串。

如果format是null或空字串,則會使用一般格式規範'G'。

此方法的部分用途包括: 取得字串,以目前文化特性的簡短日期和時間格式顯示日期和時間。

若要這樣做,請使用"G"格式規範。

取得僅包含月份和年份的字串。

若要這樣做,請使用"MM/yyyy"格式字串。

格式字串會使用目前文化特性的日期分隔符號。

取得包含特定格式之日期和時間的字串。

例如,"MM/dd/yyyyHH:MM"格式字串會以固定格式顯示日期和時間字串,例如"19//03//201318:06"。

格式字串會使用"/"做為固定日期分隔符號,而不論文化特性特定的設定為何。

取得可用於序列化日期字串之壓縮格式的日期。

例如,"yyyyMMdd"格式字串會顯示四位數年份,後面接著兩位數的月份和兩位數的天數,不含日期分隔符號。

下列範例會使用這三個格式字串,藉由使用en-us和fr-fr文化特性的慣例來顯示日期和時間值。

usingSystem; usingSystem.Globalization; publicclassToStringExample5 { publicstaticvoidMain3() { string[]formats={"G","MM/yyyy",@"MM\/dd\/yyyyHH:mm","yyyyMMdd"}; string[]cultureNames={"en-US","fr-FR"}; DateTimedate=newDateTime(2015,8,18,13,31,17); foreach(varcultureNameincultureNames) { varculture=newCultureInfo(cultureName); CultureInfo.CurrentCulture=culture; Console.WriteLine(culture.NativeName); foreach(varformatinformats) Console.WriteLine($"{format}:{date.ToString(format)}"); Console.WriteLine(); } } } //Theexampledisplaysthefollowingoutput: //English(UnitedStates) //G:8/18/20151:31:17PM //MM/yyyy:08/2015 //MM\/dd\/yyyyHH:mm:08/18/201513:31 //yyyyMMdd:20150818 // //français(France) //G:18/08/201513:31:17 //MM/yyyy:08/2015 //MM\/dd\/yyyyHH:mm:08/18/201513:31 //yyyyMMdd:20150818 openSystem openSystem.Globalization letformats=["G";"MM/yyyy";@"MM\/dd\/yyyyHH:mm";"yyyyMMdd"] letcultureNames=["en-US";"fr-FR"] letdate=DateTime(2015,8,18,13,31,17) forcultureNameincultureNamesdo letculture=CultureInfocultureName CultureInfo.CurrentCulturestring PublicFunctionToString(providerAsIFormatProvider)AsString 參數 provider IFormatProvider 物件,提供特定文化特性格式資訊。

傳回 String 目前DateTime物件值的字串表示,如provider所指定。

實作 ToString(IFormatProvider) 例外狀況 ArgumentOutOfRangeException 日期和時間超出provider使用之曆法所支援的日期範圍。

範例 下列範例CultureInfo會使用代表五種不同文化特性的物件,來顯示日期和時間的字串表示。

usingSystem; usingSystem.Globalization; publicclassToStringExample3 { publicstaticvoidMain2() { CultureInfo[]cultures=newCultureInfo[]{CultureInfo.InvariantCulture, newCultureInfo("en-us"), newCultureInfo("fr-fr"), newCultureInfo("de-DE"), newCultureInfo("es-ES"), newCultureInfo("ja-JP")}; DateTimethisDate=newDateTime(2009,5,1,9,0,0); foreach(CultureInfocultureincultures) { stringcultureName; if(string.IsNullOrEmpty(culture.Name)) cultureName=culture.NativeName; else cultureName=culture.Name; Console.WriteLine("In{0},{1}", cultureName,thisDate.ToString(culture)); } } } //Theexampleproducesthefollowingoutput: //InInvariantLanguage(InvariantCountry),05/01/200909:00:00 //Inen-US,5/1/20099:00:00AM //Infr-FR,01/05/200909:00:00 //Inde-DE,01.05.200909:00:00 //Ines-ES,01/05/20099:00:00 //Inja-JP,2009/05/019:00:00 openSystem openSystem.Globalization letcultures= [CultureInfo.InvariantCulture CultureInfo"en-us" CultureInfo"fr-fr" CultureInfo"de-DE" CultureInfo"es-ES" CultureInfo"ja-JP"] letthisDate=DateTime(2009,5,1,9,0,0) forcultureinculturesdo letcultureName= ifString.IsNullOrEmptyculture.Namethen culture.NativeName else culture.Name printfn$"In{cultureName},{thisDate.ToStringculture}" //Theexampleproducesthefollowingoutput: //InInvariantLanguage(InvariantCountry),05/01/200909:00:00 //Inen-US,5/1/20099:00:00AM //Infr-FR,01/05/200909:00:00 //Inde-DE,01.05.200909:00:00 //Ines-ES,01/05/20099:00:00 //Inja-JP,2009/05/019:00:00 ImportsSystem.Globalization ModuleExample4 PublicSubMain4() Dimcultures()AsCultureInfo={CultureInfo.InvariantCulture, NewCultureInfo("en-us"), NewCultureInfo("fr-fr"), NewCultureInfo("de-DE"), NewCultureInfo("es-ES"), NewCultureInfo("ja-JP")} DimthisDateAsDate=#5/1/20099:00AM# ForEachcultureAsCultureInfoIncultures DimcultureNameAsString IfString.IsNullOrEmpty(culture.Name)Then cultureName=culture.NativeName Else cultureName=culture.Name EndIf Console.WriteLine("In{0},{1}", cultureName,thisDate.ToString(culture)) Next EndSub EndModule 'Theexampleproducesthefollowingoutput: 'InInvariantLanguage(InvariantCountry),05/01/200909:00:00 'Inen-US,5/1/20099:00:00AM 'Infr-FR,01/05/200909:00:00 'Inde-DE,01.05.200909:00:00 'Ines-ES,01/05/20099:00:00 'Inja-JP,2009/05/019:00:00 備註 目前物件的值DateTime會使用一般日期和時間格式規範來格式化('G'),其使用簡短日期模式和完整時間模式來格式化輸出。

簡短日期和完整時間模式的格式是由參數所定義provider。

provider參數可以是下列任何一項: CultureInfo物件,表示要在傳回的字串中反映其格式設定慣例的文化特性。

DateTimeFormatInfo由屬性傳回的物件會CultureInfo.DateTimeFormat定義傳回之字串的格式。

DateTimeFormatInfo定義日期和時間資料格式的物件。

實作為介面的自訂物件IFormatProvider。

它的GetFormat方法DateTimeFormatInfo會傳回提供格式資訊的物件。

如果provider為null,則DateTimeFormatInfo會使用與目前文化特性相關聯的物件。

如需詳細資訊,請參閱CultureInfo.CurrentCulture。

給呼叫者的注意事項 方法會傳回由ToString(IFormatProvider)參數所代表之文化特性所使用行事曆中日期和時間的字串表示provider。

它的行事曆是由Calendar屬性所定義。

如果目前實例的值DateTime早于MinSupportedDateTime或晚于MaxSupportedDateTime,則方法會擲回ArgumentOutOfRangeException。

下列範例提供說明。

它會嘗試將類別範圍以外的日期格式化JapaneseCalendar。

:::codelanguage="csharp"source="~/samples/snippets/csharp/System/DateTime/ToString/datetime.tostring.argumentoutofrangeexception1.cs"interactive="try-dotnet"id="Snippet1"::::::codelanguage="fsharp.core"source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.datetime.tostring.argumentoutofrangeexception/fs/datetime.tostring.argumentoutofrangeexception1.fs"id="Snippet1"::::::codelanguage="vb"source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.string.tostring.argumentoutofrangeexception/vb/datetime.argumentoutofrangeexception1.vb"id="Snippet1"::: 另請參閱 DateTimeFormatInfo CultureInfo 適用於 ToString() 使用目前文化特性的格式化慣例,將目前DateTime物件的值轉換為其相等的字串表示。

public: overrideSystem::String^ToString(); publicoverridestringToString(); overridethis.ToString:unit->string PublicOverridesFunctionToString()AsString 傳回 String 目前DateTime物件值的字串表示。

例外狀況 ArgumentOutOfRangeException 日期和時間超出目前文化特性使用之曆法所支援的日期範圍。

範例 下列範例說明方法所傳回之值的字串表示如何DateTime取決於ToString()執行緒目前的文化特性。

它會將目前的文化特性變更為en-us、fr-fr和ja-jp,而在每個案例中,會呼叫ToString()方法,以傳回使用該文化特性之日期和時間值的字串表示。

usingSystem; usingSystem.Globalization; publicclassToStringExample1 { publicstaticvoidMain() { CultureInfocurrentCulture=CultureInfo.CurrentCulture; DateTimeexampleDate=newDateTime(2021,5,1,18,32,6); //Changethecurrentculturetoen-USanddisplaythedate. CultureInfo.CurrentCulture=CultureInfo.GetCultureInfo("en-US"); Console.WriteLine(exampleDate.ToString()); //Changethecurrentculturetofr-FRanddisplaythedate. CultureInfo.CurrentCulture=CultureInfo.GetCultureInfo("fr-FR"); Console.WriteLine(exampleDate.ToString()); //Changethecurrentculturetoja-JPanddisplaythedate. CultureInfo.CurrentCulture=CultureInfo.GetCultureInfo("ja-JP"); Console.WriteLine(exampleDate.ToString()); //Restoretheoriginalculture CultureInfo.CurrentCulture=currentCulture; } } //Theexampledisplaysthefollowingoutputtotheconsole: //5/1/20216:32:06PM //01/05/202118:32:06 //2021/05/0118:32:06 openSystem openSystem.Globalization letcurrentCulture=CultureInfo.CurrentCulture letexampleDate=DateTime(2021,5,1,18,32,6) //Changethecurrentculturetoen-USanddisplaythedate. CultureInfo.CurrentCulture



請為這篇文章評分?