// 13 August
string americanDate = theDate.ToString("m", new CultureInfo("en-us"));
// August 13
string ukDate = theDate.ToString("m", new CultureInfo("en-gb"));
有關可用格式的完整列表,請參閱MSDN。
編輯:
在您的應用程序,不用手動指定CultureInfo
實例:使用默認的培養DateTime.ToString
,這是執行的線程的文化。
所以,你的代碼就變成了:
string cultureSpecificDate = theDate.ToString("m");
其中"m"
是你可用的格式列表中選擇一個日期格式。你想要的那個看起來不是由DateTimeFormatInfo
類提供的標準。你不能在下面的列表中選擇一個受支持的模式嗎?
/*
This code produces the following output.
FORMAT en-US EXAMPLE
CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY
d 1/3/2002
M/d/yyyy (ShortDatePattern)
D Thursday, January 03, 2002
dddd, MMMM dd, yyyy (LongDatePattern)
f Thursday, January 03, 2002 12:00 AM
F Thursday, January 03, 2002 12:00:00 AM
dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
g 1/3/2002 12:00 AM
G 1/3/2002 12:00:00 AM
m January 03
MMMM dd (MonthDayPattern)
M January 03
MMMM dd (MonthDayPattern)
o 2002-01-03T00:00:00.0000000
r Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
R Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
s 2002-01-03T00:00:00
yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
t 12:00 AM
h:mm tt (ShortTimePattern)
T 12:00:00 AM
h:mm:ss tt (LongTimePattern)
u 2002-01-03 00:00:00Z
yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
U Thursday, January 03, 2002 8:00:00 AM
y January, 2002
MMMM, yyyy (YearMonthPattern)
Y January, 2002
MMMM, yyyy (YearMonthPattern)
*/
在解析DateTime時不能指定CultureInfo嗎? – h1ghfive 2012-08-13 15:06:09