C#格式化字符串

    在C#中也经常使用这个方法来格式化字符串,比如下面这样:

int x = 16;
decimal y = 3.57m;
string h = String.Format( "item {0} sells at {1:C}", x, y );
Console.WriteLine(h);

    在我的机器上,可以得到下面的输出:

item 16 sells at ¥3.57

    也许你的机器上的输出和这个不太一样。这是正常的,本文稍后就会解释这个问题。

    在我们日常使用中,更多的是使用Console.WriteLine方法来输出一个字符串。其实String.Format和Console.WriteLine有很多共同点。两个方法都有很多重载的格式并且采用无固定参数的对象数组作为最后一个参数。下面的两个语句会产生同样的输出。

Console.WriteLine( "Hello {0} {1} {2} {3} {4} {5} {6} {7} {8}", 123, 45.67, true, 'Q', 4, 5, 6, 7, '8');
string u = String.Format("Hello {0} {1} {2} {3} {4} {5} {6} {7} {8}", 123, 45.67, true, 'Q', 4, 5, 6, 7, '8');
Console.WriteLine(u);

    输出如下:

Hello 123 45.67 True Q 4 5 6 7 8
Hello 123 45.67 True Q 4 5 6 7 8

    2 字符串格式

    String.Format和WriteLine都遵守同样的格式化规则。格式化的格式如下:"{ N [, M ][: formatString ]}", arg1, ... argN,在这个格式中:

    1) N是从0开始的整数,表示要格式化的参数的个数

    2) M是一个可选的整数,表示格式化后的参数所占的宽度,如果M是负数,那么格式化后的值就是左对齐的,如果M是正数,那么格式化后的值是右对齐的

    3) formatString是另外一个可选的参数,表示格式代码

    argN表示要格式化的表达式,和N是对应的。

    如果argN是空值,那么就用一个空字符串来代替。如果没有formatString,那么就用参数N对应的ToString方法来格式化。下面的语句会产生同样的输出:

public class TestConsoleApp
{
    public static void Main(string[] args)
    {
        Console.WriteLine(123);
        Console.WriteLine("{0}", 123);
        Console.WriteLine("{0:D3}", 123);
    }
}

    输出是:

123
123
123

 也可以通过String.Format得到同样的输出。

string s = string.Format("123");
string t = string.Format("{0}", 123);
string u = string.Format("{0:D3}", 123);
Console.WriteLine(s);
Console.WriteLine(t);
Console.WriteLine(u);

    因此有如下结论:

    (,M)决定了格式化字符串的宽度和对齐方向

    (:formatString)决定了如何格式化数据,比如用货币符号,科学计数法或者16进制。就像下面这样:

Console.WriteLine("{0,5} {1,5}", 123, 456);      // 右对齐
Console.WriteLine("{0,-5} {1,-5}", 123, 456);    // 左对齐

    输出是:

000123     000456

    我们可以用这种格式化特性来对齐我们的输出。

Console.WriteLine("\n{0,-10}{1,-3}", "Name","Salary");
Console.WriteLine("----------------");
Console.WriteLine("{0,-10}{1,6}", "Bill", 123456);
Console.WriteLine("{0,-10}{1,6}", "Polly", 7890);

    输出是:

Name      Salary
----------------
Bill      123456
Polly       7890

 3 格式化标识符

    标准的数学格式字符串用于返回通常使用的字符串。它们通常象X0这样的格式。X是格式化标识符,0是精度标识符。格式标识符号共有9种,它们代表了大多数常用的数字格式。就像下表所示:

字母  含义
C或c Currency 货币格式
D或d Decimal 十进制格式(十进制整数,不要和.Net的Decimal数据类型混淆了)
E或e Exponent 指数格式
F或f Fixed point 固定精度格式
G或g General 常用格式
N或n 用逗号分割千位的数字,比如1234将会被变成1,234
P或p Percentage 百分符号格式
R或r Round-trip  圆整(只用于浮点数)保证一个数字被转化成字符串以后可以再被转回成同样的数字
X或x Hex 16进制格式


    如果我们使用下面的表达方式,让我们看看会发生什么

public class FormatSpecApp
{
    public static void Main(string[] args)
    {
        int i = 123456;
        Console.WriteLine("{0:C}", i); // ¥123,456.00
        Console.WriteLine("{0:D}", i); // 123456
        Console.WriteLine("{0:E}", i); // 1.234560E+005
        Console.WriteLine("{0:F}", i); // 123456.00
        Console.WriteLine("{0:G}", i); // 123456
        Console.WriteLine("{0:N}", i); // 123,456.00
        Console.WriteLine("{0:P}", i); // 12,345,600.00 %
        Console.WriteLine("{0:X}", i); // 1E240
    }
}

    精度控制标识控制了有效数字的个数或者十进制数小数的位数。

Console.WriteLine("{0:C5}", i); // ¥123,456.00
Console.WriteLine("{0:D5}", i); // 123456
Console.WriteLine("{0:E5}", i); // 1.23456E+005
Console.WriteLine("{0:F5}", i); // 123456.00000
Console.WriteLine("{0:G5}", i); // 1.23456E5
Console.WriteLine("{0:N5}", i); // 123,456.00000
Console.WriteLine("{0:P5}", i); // 12,345,600.00000 %
Console.WriteLine("{0:X5}", i); // 1E240

    R(圆整)格式仅仅对浮点数有效。这个值首先会用通用格式来格式化。对于双精度数有15位精度,对于单精度数有7位精度。如果这个值可以被正确地解析回原始的数字,就会用通用格式符来格式化。如果不能解析回去的话,那么就会用17位精度来格式化双精度数,用9位精度来格式化单精度数。尽管我们可以在圆整标识符后面添加有效数字的位数,但是它会被忽略掉。

double d = 1.2345678901234567890;
Console.WriteLine("Floating-Point:\t{0:F16}", d);  // 1.2345678901234600
Console.WriteLine("Roundtrip:\t{0:R16}", d);       // 1.2345678901234567

    如果标准格式化标识符还不能满足你。你可以使用图形化格式字符串来创建定制的字符串输出。图形化格式化使用占位符来表示最小位数,

    最大位数,定位符号,负号的外观以及其它数字符号的外观。就像下表所示

 符号 名称 含义
0 0占位符 用0填充不足的位数
# 数字占位符 用#代替实际的位数
. 十进制小数点  
, 千位分隔符 用逗号进行千位分割,比如把1000分割成1,000
% 百分符号 显示一个百分标识
E+0
E-0
e+0
e-0
指数符号 用指数符号格式化输出
\ 专一字符 用于传统格式的格式化序列,比如"\n"(新行)
'ABC'
"ABC"
常量字符串  显示单引号或者双引号里面的字符串
; 区域分隔符  如果数字会被格式化成整数,负数,或者0,用;来进行分隔
,. 缩放符号 数字除以1000


    看下面的例子:

double i = 123456.42;
Console.WriteLine();
Console.WriteLine("{0:000000.00}", i); //123456.42
Console.WriteLine("{0:00.00000000e+0}", i); //12.34564200e+4
Console.WriteLine("{0:0,.}", i);          //123
Console.WriteLine("{0:#0.000}", i);             // 123456.420
Console.WriteLine("{0:#0.000;(#0.000)}", i);        // 123456.420
Console.WriteLine("{0:#0.000;(#0.000);<zero>}", i); // 123456.420
Console.WriteLine("{0:#%}", i);     // 12345642%

i = -123456.42;
Console.WriteLine();
Console.WriteLine("{0:000000.00}", i); //-123456.42
Console.WriteLine("{0:00.00000000e+0}", i); //-12.34564200e+4
Console.WriteLine("{0:0,.}", i);          //-123
Console.WriteLine("{0:#0.000}", i);             // -123456.420
Console.WriteLine("{0:#0.000;(#0.000)}", i);        // (123456.420)
Console.WriteLine("{0:#0;(#0);<zero>}", i); // (123456)
Console.WriteLine("{0:#%}", i);             // -12345642%

i = 0;
Console.WriteLine();
Console.WriteLine("{0:0,.}", i);          //0
Console.WriteLine("{0:#0}", i);             // 0
Console.WriteLine("{0:#0;(#0)}", i);        // 0
Console.WriteLine("{0:#0;(#0);<zero>}", i); // <zero>
Console.WriteLine("{0:#%}", i);             // %

    4 数字字符串的解析

    所有的基础类型都有ToString方法,它是从object类型中继承过来的。所有的数值类型都有Parse方法,它用字符串为参数,并且返回相等的数值。比如

public class NumParsingApp
{
    public static void Main(string[] args)
    {
        int i = int.Parse("12345");
        Console.WriteLine("i = {0}", i);

        int j = Int32.Parse("12345");
        Console.WriteLine("j = {0}", j);

        double d = Double.Parse("1.2345E+6");
        Console.WriteLine("d = {0:F}", d);

        string s = i.ToString();
        Console.WriteLine("s = {0}", s);
    }
}

    输出如下:

i = 12345
j = 12345
d = 1234500.00
s = 12345

    在缺省状况下,某些非数字字符是可以存在的。比如开头和结尾的空白。逗号和小数点,加号和减号,因此,下面的Parse语句是一样的

string t = "  -1,234,567.890  ";
//double g = double.Parse(t);        // 和下面的代码干同样的事情
double g = double.Parse(t, 
    NumberStyles.AllowLeadingSign | 
    NumberStyles.AllowDecimalPoint |
    NumberStyles.AllowThousands |
    NumberStyles.AllowLeadingWhite | 
    NumberStyles.AllowTrailingWhite);
Console.WriteLine("g = {0:F}", g);

  输出都是这样:

g = -1234567.89

  注意到,如果你要使用NumberStyles,就要添加对System.Globalization的引用,然后就可以使用不同NumberStyles的组合或者其中的任意一种。如果你想兼容货币符号,就需要使用重载的Parse方法,它们采用了NumberFormatInfo对象作为一个参数,然后你可以设置NumberFormatInfo的CurrencySymbol属性来调用Parse方法,比如:

string u = "¥  -1,234,567.890  ";
NumberFormatInfo ni = new NumberFormatInfo();
ni.CurrencySymbol = "";
double h = Double.Parse(u, NumberStyles.Any, ni);
Console.WriteLine("h = {0:F}", h);

  上面的代码有如下输出:

h = -1234567.89

  除了NumberFormatInfo,还可以使用CultureInfo类。CultureInfo代表了某种特定的文化,包括文化的名字,书写的方式,日历的格式。对于某种特定文化的操作是非常普遍的情况,比如格式化日期和排序。文化的命名方式遵从RFC1766标准,使用<语言代码2>-<国家/地区码2>的方式,其中的<语言代码2>是两个小写的字母,它们来自ISO639-1;<国家/地区码2>是两个大写字母,它们来自ISO3166。比如,美国英语是“en-US"。英国英语是"en-GB"。特立尼达和多巴哥英语是"en-TT"。例如,我们可以创建一个美国英语的CultureInfo对象并且基于这种文化将数字转换成字符串。

int k = 12345;
CultureInfo us = new CultureInfo("en-US");
string v = k.ToString("c", us);
Console.WriteLine(v);

  输出是:

$12,345.00

  要注意到,我们使用了重载的ToString方法,它把第一个格式化字符串当成第一个参数,将一个CultureInfo对象(执行了IFormatProvider对象)作为第二个参数。这儿有第二个例子,对于丹麦人来说:

CultureInfo dk = new CultureInfo("da-DK");
string w = k.ToString("c", dk);
Console.WriteLine(w);

  输出是:

kr 12.345,00

  5 字符串和日期

  一个日期对象有个叫Ticks的属性。它存储了自从公元1年的1月1号上午12点开始的,以100纳秒为间隔的时间。比如,Ticks值等于31241376000000000L表示公元100年,星期五,1月1号,上午12点这一时间。Ticks总是以100纳秒为间隔递增。

  DateTime的值以存储在DateTimeFormatInfo实例里面的标准或者自定义的方式来表示。为了修改一个日期显示的方式,DateTimeFormatInfo实例必须要是可写的,以便我们写入自定义的格式并且存入属性中

using System.Globalization;

public class DatesApp
{
    public static void Main(string[] args)
    {
        DateTime dt = DateTime.Now;
        Console.WriteLine(dt);
        Console.WriteLine("date = {0}, time = {1}\n",
            dt.Date, dt.TimeOfDay);
    }
}

  代码会产生下面的输出:

23/06/2001 17:55:10
date = 23/06/2001 00:00:00, time = 17:55:10.3839296

  下表列出了标准的格式字符串以及相关的DateTimeFormatInfo属性

D    
D MM/dd/yyyy ShortDatePattern(短日期模式)
D dddd,MMMM dd,yyyy    LongDatePattern(长日期模式)
F dddd,MMMM dd,yyyy HH:mm Full date and time (long date and short time)(全日期和时间模式)
F dddd,MMMM dd,yyyy HH:mm:ss FullDateTimePattern (long date and long time)(长日期和长时间)
G MM/dd/yyyy HH:mm General (short date and short time)(通用模式,短日期和短时间)
G MM/dd/yyyy HH:mm:ss General (short date and long time)(通用模式,短日期和长时间)
M,M MMMM dd  MonthDayPattern(月天模式)
r,R ddd,dd MMM yyyy,HH':'mm':'ss 'GMT' RFC1123Pattern (RFC1123模式)
S yyyy-MM-dd HH:mm:ss  SortableDateTimePattern (conforms to ISO 8601) using local time(使用本地时间的可排序模式)
T HH:mm  ShortTimePattern (短时间模式)
T HH:mm:ss LongTimePattern(长时间模式)
U yyyy-MM-dd HH:mm:ss UniversalSortable-DateTimePattern (conforms to ISO 8601) using universal time(通用可排序模式)
U dddd,MMMM dd,yyyy,HH:mm:ss UniversalSortable-DateTimePattern(通用可排序模式)
y,Y MMMM,yyyy YearMonthPattern(年月模式)


  DateTimeFormatInfo.InvariantInfo属性得到了默认的只读的DateTimeFormatInfo实例,它与文化无关。你可以创建自定义的模式。要注意到的是InvariantInfo不一定和本地的格式一样。Invariant等于美国格式。另外,如果你向DateTime.Format方法传递的第二个参数是null,DateTimeFormatInfo将会是默认的CurrentInfo。比如

Console.WriteLine(dt.ToString("d", dtfi));
Console.WriteLine(dt.ToString("d", null));
Console.WriteLine();

  输出是:

06/23/2001
23/06/2001

  对比选择InvariantInfo和CurrentInfo的。

DateTimeFormatInfo dtfi;
Console.Write("[I]nvariant or [C]urrent Info?: ");
if (Console.Read() == 'I')
    dtfi = DateTimeFormatInfo.InvariantInfo;
else
    dtfi = DateTimeFormatInfo.CurrentInfo;
DateTimeFormatInfo dtfi = DateTimeFormatInfo.InvariantInfo;
Console.WriteLine(dt.ToString("D", dtfi));
Console.WriteLine(dt.ToString("f", dtfi));
Console.WriteLine(dt.ToString("F", dtfi));
Console.WriteLine(dt.ToString("g", dtfi));
Console.WriteLine(dt.ToString("G", dtfi));
Console.WriteLine(dt.ToString("m", dtfi));
Console.WriteLine(dt.ToString("r", dtfi));
Console.WriteLine(dt.ToString("s", dtfi));
Console.WriteLine(dt.ToString("t", dtfi));
Console.WriteLine(dt.ToString("T", dtfi));
Console.WriteLine(dt.ToString("u", dtfi));
Console.WriteLine(dt.ToString("U", dtfi));
Console.WriteLine(dt.ToString("d", dtfi));
Console.WriteLine(dt.ToString("y", dtfi));
Console.WriteLine(dt.ToString("dd-MMM-yy", dtfi));

  输出是:

[I]nvariant or [C]urrent Info?: I
01/03/2002
03/01/2002

Thursday, 03 January 2002
Thursday, 03 January 2002 12:55
Thursday, 03 January 2002 12:55:03
01/03/2002 12:55
01/03/2002 12:55:03
January 03
Thu, 03 Jan 2002 12:55:03 GMT
2002-01-03T12:55:03
12:55
12:55:03
2002-01-03 12:55:03Z
Thursday, 03 January 2002 12:55:03
01/03/2002
2002 January
03-Jan-02


[I]nvariant or [C]urrent Info?: C
03/01/2002
03/01/2002

03 January 2002
03 January 2002 12:55
03 January 2002 12:55:47
03/01/2002 12:55
03/01/2002 12:55:47
03 January
Thu, 03 Jan 2002 12:55:47 GMT
2002-01-03T12:55:47
12:55
12:55:47
2002-01-03 12:55:47Z
03 January 2002 12:55:47
03/01/2002
January 2002
03-Jan-02

  

int a = 12345678;
//格式为sring输出
Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);
Label2.Text = "asdfadsf"+a.ToString()+"adsfasdf";
Label1.Text = string.Format("asdfadsf{0:C}adsfasdf",a);//asdfadsf¥1,234.00adsfasdf
Label2.Text = "asdfadsf"+a.ToString("C")+"adsfasdf";//asdfadsf¥1,234.00adsfasdf
double b = 1234.12543;
int a = 12345678;

//格式为特殊的string样式输出
Label1.Text = string.Format("asdfadsf{0:C}adsfasdf",b);//asdfadsf¥1,234.13adsfasdf
Label2.Text = "asdfadsf"+b.ToString("C")+"adsfasdf";//asdfadsf¥1,234.13adsfasdf
Label1.Text = string.Format("{0:C3}",b);//¥1,234.125
Label2.Text = b.ToString("C3");//¥1,234.125
Label1.Text = string.Format("{0:d}",a);//十进制--12345678
Label2.Text = b.ToString("d");//十进制--相同的类型,转换报错
Label1.Text = string.Format("{0:e}",a);//指数--1.234568e+007
Label2.Text = b.ToString("e");//指数--1.234125e+003
Label1.Text = string.Format("{0:f}",a);//定点数--12345678.00
Label2.Text = b.ToString("f");//定点数--1234.13
Label1.Text = string.Format("{0:n}",a);//数值--12,345,678.00
Label2.Text = b.ToString("n");//数值--1,234.13
Label1.Text = string.Format("{0:x}",a);//十六进制--bc614e
Label2.Text = b.ToString("x");//16--带有小数不能转换,出错
Label1.Text = string.Format("{0:g}",a);//通用为最紧凑--12345678
Label2.Text = b.ToString("g");//通用为最紧凑--1234.12543
Label1.Text = string.Format("{0:r}",a);//转来转去不损失精度--整数不允许用,报错
Label2.Text = b.ToString("r");//转来转去不损失精度--1234.12543

double b = 4321.12543;
int a = 1234;
自定义模式输出:

//"0"描述:占位符,如果可能,填充位
Label1.Text = string.Format("{0:000000}",a);// 001234
Label2.Text = string.Format("{0:000000}",b);// 004321

//"#"描述:占位符,如果可能,填充位
Label1.Text = string.Format("{0:#######}",a);// 1234
Label2.Text = string.Format("{0:#######}",b);// 4321
Label1.Text = string.Format("{0:#0####}",a);// 01234
Label2.Text = string.Format("{0:0#0000}",b);// 004321

//"."描述:小数点
Label1.Text = string.Format("{0:000.000}",a);//1234.000
Label2.Text = string.Format("{0:000.000}",b);//4321.125
double b = 87654321.12543;
int a = 12345678;

//","描述:数字分组,也用于增倍器
Label1.Text = string.Format("{0:0,00}",a);// 12,345,678
Label2.Text = string.Format("{0:0,00}",b);// 87,654,32
Label1.Text = string.Format("{0:0,}",a);// 12346
Label2.Text = string.Format("{0:0,}",b);// 87654
Label1.Text = string.Format("{0:0,,}",a);// 12
Label2.Text = string.Format("{0:0,,}",b);// 88
Label1.Text = string.Format("{0:0,,,}",a);// 0
Label2.Text = string.Format("{0:0,,,}",b);// 0

//"%"描述:格式为百分数
Label1.Text = string.Format("{0:0%}",a);// 1234567800%
Label2.Text = string.Format("{0:#%}",b);// 8765432113%
Label1.Text = string.Format("{0:0.00%}",a);// 1234567800.00%
Label2.Text = string.Format("{0:#.00%}",b);// 8765432112.54%

//"abc"描述:显示单引号内的文本
Label1.Text = string.Format("{0:'文本'0}",a);// 文本12345678
Label2.Text = string.Format("{0:文本0}",b);// 文本87654321

//"""描述:后跟1要打印字的字符,也用于转移符"n等
Label1.Text = string.Format("""你好!""");// "你好!"
Label2.Text = string.Format("[url=file://""c""books""new""we.asp]""c""books""new""we.asp");//"c"books"new"we.asp

//"@"描述:后跟要打印字的字符,
Label1.Text = string.Format(@"""你好!"""); // "你好!"要打印"则需要输入两对才可以
Label2.Text = string.Format(@""c"books"new"we.asp");//"c"books"new"we.asp

Label1.Text = String.Format("{0:yyyy年-mm月-dd日",DateTime.Now);

日期转化一

为了达到不同的显示效果有时,我们需要对时间进行转化,默认格式为:2007-01-03 14:33:34 ,要转化为其他格式,要用到DateTime.ToString的方法(String, IFormatProvider),如下所示:

using System;
using System.Globalization;
String format="D";
DateTime date=DataTime,Now;
Response.Write(date.ToString(format, DateTimeFormatInfo.InvariantInfo));

结果输出
Thursday, June 16, 2005

参数format格式详细用法:

格式字符 关联属性/说明
d ShortDatePattern
D LongDatePattern
f 完整日期和时间(长日期和短时间)
F FullDateTimePattern(长日期和长时间)
g 常规(短日期和短时间)
G 常规(短日期和长时间)
m、M MonthDayPattern
r、R RFC1123Pattern
s 使用当地时间的 SortableDateTimePattern(基于 ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern 用于显示通用时间的格式
U 使用通用时间的完整日期和时间(长日期和长时间)
y、Y YearMonthPattern

下表列出了可被合并以构造自定义模式的模式。这些模式是区分大小写的;例如,识别“MM”,但不识别“mm”。如果自定义模式包含空白字符或用单引号括起来的字符,则输出字符串页也将包含这些字符。未定义为格式模式的一部分或未定义为格式字符的字符按其原义复制。

格式模式 说明
d 月中的某一天。一位数的日期没有前导零。
dd 月中的某一天。一位数的日期有一个前导零。
ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
dddd 周中某天的完整名称,在 DayNames 中定义。
M 月份数字。一位数的月份没有前导零。
MM 月份数字。一位数的月份有一个前导零。
MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。
MMMM 月份的完整名称,在 MonthNames 中定义。
y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。
yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
yyyy 包括纪元的四位数的年份。
gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
h 12 小时制的小时。一位数的小时数没有前导零。
hh 12 小时制的小时。一位数的小时数有前导零。
H 24 小时制的小时。一位数的小时数没有前导零。
HH 24 小时制的小时。一位数的小时数有前导零。
m 分钟。一位数的分钟数没有前导零。
mm 分钟。一位数的分钟数有一个前导零。
s 秒。一位数的秒数没有前导零。
ss 秒。一位数的秒数有一个前导零。
f 秒的小数精度为一位。其余数字被截断。
ff 秒的小数精度为两位。其余数字被截断。
fff 秒的小数精度为三位。其余数字被截断。
ffff 秒的小数精度为四位。其余数字被截断。
fffff 秒的小数精度为五位。其余数字被截断。
ffffff 秒的小数精度为六位。其余数字被截断。
fffffff 秒的小数精度为七位。其余数字被截断。
t 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。
tt 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。
z 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。
zz 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。
zzz 完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。
: 在 TimeSeparator 中定义的默认时间分隔符。
/ 在 DateSeparator 中定义的默认日期分隔符。
% c 其中 c 是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
" c 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“""”。

只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。自定义模式的长度至少为两个字符;例如,

DateTime.ToString( "d") 返回 DateTime 值;“d”是标准短日期模式。
DateTime.ToString( "%d") 返回月中的某天;“%d”是自定义模式。
DateTime.ToString( "d ") 返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。

比较方便的是,上面的参数可以随意组合,并且不会出错,多试试,肯定会找到你要的时间格式
如要得到2005年06月 这样格式的时间
可以这样写:
date.ToString("yyyy年MM月", DateTimeFormatInfo.InvariantInfo)

posted @ 2013-02-17 14:30  ludard  阅读(168)  评论(0)    收藏  举报