Tuesday, July 2nd, 2009 的通用格式 ??

mqc507 2009-07-21 04:32:21
DateTime.Parse("2009-1-1").ToString("dddd MMMM dd, yyyy");
上面的格式得到的是 :xxxx xxx 01, 2009
我想得到 : xxxx xxx 1st, 2009 怎么格式 ?
...全文
80 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2009-07-21
  • 打赏
  • 举报
回复
格式全球化
DateTime dt = DateTime.Now;
String[] format = {"d","D","f","F","g","G","m","r","s","t", "T","u", "U","y","dddd, MMMM dd yyyy","ddd, MMM d \"'\"yy","dddd, MMMM dd","M/yy","dd-MM-yy",};
String date;
for (int i = 0; i < format.Length; i++)
{
date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
Console.WriteLine(String.Concat(format[i], " :" , date));
}
}

d :07/11/2009
D :Sunday, 11 July 2009
f :Sunday, 11 July 2009 10:52
F :Sunday, 11 July 2009 10:52:36
g :07/11/2009 10:52
G :07/11/2009 10:52:36
m :July 11
r :Sun, 11 Jul 2009 10:52:36 GMT
s :2009 -07-11T10:52:36
t :10:52
T :10:52:36
u :2004-07-11 10:52:36Z
U :Sunday, 11 July 2009 02:52:36
y :2009 July
dddd, MMMM dd yyyy :Sunday, July 11 2009
ddd, MMM d "'"yy :Sun, Jul 11 '04
dddd, MMMM dd :Sunday, July 11
M/yy :7/04
dd-MM-yy :11-07-04
参考
参考
周公 2009-07-21
  • 打赏
  • 举报
回复
下面的代码示例使用线程当前区域性、指定的区域性及所有标准日期和时间格式说明符设置 DateTime 对象的格式。

// This code example demonstrates the ToString(String) and
// ToString(String, IFormatProvider) methods for the DateTime
// type in conjunction with the standard date and time
// format specifiers.

using System;
using System.Globalization;
using System.Threading;

class Sample
{
public static void Main()
{
string msgShortDate = "(d) Short date: . . . . . . . ";
string msgLongDate = "(D) Long date:. . . . . . . . ";
string msgShortTime = "(t) Short time: . . . . . . . ";
string msgLongTime = "(T) Long time:. . . . . . . . ";
string msgFullDateShortTime =
"(f) Full date/short time: . . ";
string msgFullDateLongTime =
"(F) Full date/long time:. . . ";
string msgGeneralDateShortTime =
"(g) General date/short time:. ";
string msgGeneralDateLongTime =
"(G) General date/long time (default):\n" +
" . . . . . . . . . . . . . ";
string msgMonth = "(M) Month:. . . . . . . . . . ";
string msgRFC1123 = "(R) RFC1123:. . . . . . . . . ";
string msgSortable = "(s) Sortable: . . . . . . . . ";
string msgUniSortInvariant =
"(u) Universal sortable (invariant):\n" +
" . . . . . . . . . . . . . ";
string msgUniFull = "(U) Universal full date/time: ";
string msgYear = "(Y) Year: . . . . . . . . . . ";
string msgRoundtripLocal = "(o) Roundtrip (local):. . . . ";
string msgRoundtripUTC = "(o) Roundtrip (UTC):. . . . . ";
string msgRoundtripUnspecified = "(o) Roundtrip (Unspecified):. ";


string msg1 = "Use ToString(String) and the current thread culture.\n";
string msg2 = "Use ToString(String, IFormatProvider) and a specified culture.\n";
string msgCulture = "Culture:";
string msgThisDate = "This date and time: {0}\n";

DateTime thisDate = DateTime.Now;
DateTime utcDate = thisDate.ToUniversalTime();
DateTime unspecifiedDate = new DateTime(2000, 3, 20, 13, 2, 3, 0, DateTimeKind.Unspecified);
CultureInfo ci;

// Format the current date and time in various ways.
Console.Clear();
Console.WriteLine("Standard DateTime Format Specifiers:\n");
Console.WriteLine(msgThisDate, thisDate);
Console.WriteLine(msg1);

// Display the thread current culture, which is used to format the values.
ci = Thread.CurrentThread.CurrentCulture;
Console.WriteLine("{0,-30}{1}\n", msgCulture, ci.DisplayName);

Console.WriteLine(msgShortDate + thisDate.ToString("d"));
Console.WriteLine(msgLongDate + thisDate.ToString("D"));
Console.WriteLine(msgShortTime + thisDate.ToString("t"));
Console.WriteLine(msgLongTime + thisDate.ToString("T"));
Console.WriteLine(msgFullDateShortTime + thisDate.ToString("f"));
Console.WriteLine(msgFullDateLongTime + thisDate.ToString("F"));
Console.WriteLine(msgGeneralDateShortTime + thisDate.ToString("g"));
Console.WriteLine(msgGeneralDateLongTime + thisDate.ToString("G"));
Console.WriteLine(msgMonth + thisDate.ToString("M"));
Console.WriteLine(msgRFC1123 + utcDate.ToString("R"));
Console.WriteLine(msgSortable + thisDate.ToString("s"));
Console.WriteLine(msgUniSortInvariant + utcDate.ToString("u"));
Console.WriteLine(msgUniFull + thisDate.ToString("U"));
Console.WriteLine(msgYear + thisDate.ToString("Y"));
Console.WriteLine(msgRoundtripLocal + thisDate.ToString("o"));
Console.WriteLine(msgRoundtripUTC + utcDate.ToString("o"));
Console.WriteLine(msgRoundtripUnspecified + unspecifiedDate.ToString("o"));

Console.WriteLine();

// Display the same values using a CultureInfo object. The CultureInfo class
// implements IFormatProvider.
Console.WriteLine(msg2);

// Display the culture used to format the values.
ci = new CultureInfo("de-DE");
Console.WriteLine("{0,-30}{1}\n", msgCulture, ci.DisplayName);

Console.WriteLine(msgShortDate + thisDate.ToString("d", ci));
Console.WriteLine(msgLongDate + thisDate.ToString("D", ci));
Console.WriteLine(msgShortTime + thisDate.ToString("t", ci));
Console.WriteLine(msgLongTime + thisDate.ToString("T", ci));
Console.WriteLine(msgFullDateShortTime + thisDate.ToString("f", ci));
Console.WriteLine(msgFullDateLongTime + thisDate.ToString("F", ci));
Console.WriteLine(msgGeneralDateShortTime + thisDate.ToString("g", ci));
Console.WriteLine(msgGeneralDateLongTime + thisDate.ToString("G", ci));
Console.WriteLine(msgMonth + thisDate.ToString("M", ci));
Console.WriteLine(msgRFC1123 + utcDate.ToString("R", ci));
Console.WriteLine(msgSortable + thisDate.ToString("s", ci));
Console.WriteLine(msgUniSortInvariant + utcDate.ToString("u", ci));
Console.WriteLine(msgUniFull + thisDate.ToString("U", ci));
Console.WriteLine(msgYear + thisDate.ToString("Y", ci));
Console.WriteLine(msgRoundtripLocal + thisDate.ToString("o", ci));
Console.WriteLine(msgRoundtripUTC + utcDate.ToString("o", ci));
Console.WriteLine(msgRoundtripUnspecified + unspecifiedDate.ToString("o", ci));

Console.WriteLine();
}
}
/*
This code example produces the following results:

Standard DateTime Format Specifiers:

This date and time: 4/17/2006 2:22:48 PM

Use ToString(String) and the current thread culture.

Culture: English (United States)

(d) Short date: . . . . . . . 4/17/2006
(D) Long date:. . . . . . . . Monday, April 17, 2006
(t) Short time: . . . . . . . 2:22 PM
(T) Long time:. . . . . . . . 2:22:48 PM
(f) Full date/short time: . . Monday, April 17, 2006 2:22 PM
(F) Full date/long time:. . . Monday, April 17, 2006 2:22:48 PM
(g) General date/short time:. 4/17/2006 2:22 PM
(G) General date/long time (default):
. . . . . . . . . . . . . 4/17/2006 2:22:48 PM
(M) Month:. . . . . . . . . . April 17
(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:22:48 GMT
(s) Sortable: . . . . . . . . 2006-04-17T14:22:48
(u) Universal sortable (invariant):
. . . . . . . . . . . . . 2006-04-17 21:22:48Z
(U) Universal full date/time: Monday, April 17, 2006 9:22:48 PM
(Y) Year: . . . . . . . . . . April, 2006
(o) Roundtrip (local):. . . . 2006-04-17T14:22:48.2698750-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:22:48.2698750Z
(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000

Use ToString(String, IFormatProvider) and a specified culture.

Culture: German (Germany)

(d) Short date: . . . . . . . 17.04.2006
(D) Long date:. . . . . . . . Montag, 17. April 2006
(t) Short time: . . . . . . . 14:22
(T) Long time:. . . . . . . . 14:22:48
(f) Full date/short time: . . Montag, 17. April 2006 14:22
(F) Full date/long time:. . . Montag, 17. April 2006 14:22:48
(g) General date/short time:. 17.04.2006 14:22
(G) General date/long time (default):
. . . . . . . . . . . . . 17.04.2006 14:22:48
(M) Month:. . . . . . . . . . 17 April
(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:22:48 GMT
(s) Sortable: . . . . . . . . 2006-04-17T14:22:48
(u) Universal sortable (invariant):
. . . . . . . . . . . . . 2006-04-17 21:22:48Z
(U) Universal full date/time: Montag, 17. April 2006 21:22:48
(Y) Year: . . . . . . . . . . April 2006
(o) Roundtrip (local):. . . . 2006-04-17T14:22:48.2698750-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:22:48.2698750Z
(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000

*/
周公 2009-07-21
  • 打赏
  • 举报
回复
这个是根据当前计算机的时期时间格式来设置的。在某些国家里,直接运行你上面的代码就可以得到你说的答案。
在其它国家里,要给定语言背景。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧