C#Format的时间格式

时间:2010年04月12日 点击:351
// This code example demonstrates the String.Format() method.
// Formatting for this example uses the "en-US" culture.

using System;
class Sample
{
enum Color {Yellow = 1, Blue, Green};
static DateTime thisDate = DateTime.Now;

public static void Main()
{
// Store the output of the String.Format method in a string.
string s = "";

Console.Clear();

// Format a negative integer or floating-point number in various ways.
Console.WriteLine("Standard Numeric Format Specifiers");
s
= String.Format(
"(C) Currency: . . . . . . . . \n" +
"(D) Decimal:. . . . . . . . . \n" +
"(E) Scientific: . . . . . . . \n" +
"(F) Fixed point:. . . . . . . \n" +
"(G) General:. . . . . . . . . \n" +
" (default):. . . . . . . . (default = 'G')\n" +
"(N) Number: . . . . . . . . . \n" +
"(P) Percent:. . . . . . . . . \n" +
"(R) Round-trip: . . . . . . . \n" +
"(X) Hexadecimal:. . . . . . . \n",
-123, -123.45f);
Console.WriteLine(s);

// Format the current date in various ways.
Console.WriteLine("Standard DateTime Format Specifiers");
s
= String.Format(
"(d) Short date: . . . . . . . \n" +
"(D) Long date:. . . . . . . . \n" +
"(t) Short time: . . . . . . . \n" +
"(T) Long time:. . . . . . . . \n" +
"(f) Full date/short time: . . \n" +
"(F) Full date/long time:. . . \n" +
"(g) General date/short time:. \n" +
"(G) General date/long time: . \n" +
" (default):. . . . . . . . (default = 'G')\n" +
"(M) Month:. . . . . . . . . . \n" +
"(R) RFC1123:. . . . . . . . . \n" +
"(s) Sortable: . . . . . . . . \n" +
"(u) Universal sortable: . . . (invariant)\n" +
"(U) Universal sortable: . . . \n" +
"(Y) Year: . . . . . . . . . . \n",
thisDate);
Console.WriteLine(s);

// Format a Color enumeration value in various ways.
Console.WriteLine("Standard Enumeration Format Specifiers");
s
= String.Format(
"(G) General:. . . . . . . . . \n" +
" (default):. . . . . . . . (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . (flags or integer)\n" +
"(D) Decimal number: . . . . . \n" +
"(X) Hexadecimal:. . . . . . . \n",
Color.Green);
Console.WriteLine(s);
}

}

/*
This code example produces the following results:

Standard Numeric Format Specifiers
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
(default):. . . . . . . . -123 (default = 'G')
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85

Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
(default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004

Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003

*/

赞助商链接

热门内容

相关内容

联系我们

联系方式