Skip to content

C# 格式字符串

日期和时间类型

日期和时间类型(DateTimeDateTimeOffset

标准日期和时间格式字符串

  • "d" : 短日期模式。

    2009-06-15T13:45:30 -> 6/15/2009 (en-US)
    2009-06-15T13:45:30 -> 15/06/2009 (fr-FR)
    2009-06-15T13:45:30 -> 2009/06/15 (ja-JP)

  • “D” : 长日期模式。

    2009-06-15T13:45:30 -> Monday, June 15, 2009 (en-US)
    2009-06-15T13:45:30 -> 15 июня 2009 г. (ru-RU)
    2009-06-15T13:45:30 -> Montag, 15. Juni 2009 (de-DE)

  • “f” : 完整日期/时间模式(短时间)。

    2009-06-15T13:45:30 -> Monday, June 15, 2009 1:45 PM (en-US)
    2009-06-15T13:45:30 -> den 15 juni 2009 13:45 (sv-SE)
    2009-06-15T13:45:30 -> Δευτέρα, 15 Ιουνίου 2009 1:45 μμ (el-GR)

  • “F” : 完整日期/时间模式(长时间)。

    2009-06-15T13:45:30 -> Monday, June 15, 2009 1:45:30 PM (zh-CN)
    2009-06-15T13:45:30 -> den 15 juni 2009 13:45:30 (sv-SE)
    2009-06-15T13:45:30 -> Δευτέρα, 15 Ιουνίου 2009 1:45:30 μμ (el-GR)

  • “g” : 常规日期/时间模式(短时间)。

    2009-06-15T13:45:30 -> 6/15/2009 1:45 PM (en-US)
    2009-06-15T13:45:30 -> 15/06/2009 13:45 (es-ES)
    2009-06-15T13:45:30 -> 2009/6/15 13:45 (zh-CN)

  • “G” : 常规日期/时间模式(长时间)。

    2009-06-15T13:45:30 -> 6/15/2009 1:45:30 PM (en-US)
    2009-06-15T13:45:30 -> 15/06/2009 13:45:30 (es-ES)
    2009-06-15T13:45:30 -> 2009/6/15 13:45:30 (zh-CN)

  • “M”、“m” : 月/日模式。

    2009-06-15T13:45:30 -> June 15 (en-US)
    2009-06-15T13:45:30 -> 15. juni (da-DK)
    2009-06-15T13:45:30 -> 15 Juni (id-ID)

  • “O”、“o” : 往返日期/时间模式。

    • DateTime 值:

      2009-06-15T13:45:30 (DateTimeKind.Local) --> 2009-06-15T13:45:30.0000000-07:00
      2009-06-15T13:45:30 (DateTimeKind.Utc) --> 2009-06-15T13:45:30.0000000Z
      2009-06-15T13:45:30 (DateTimeKind.Unspecified) --> 2009-06-15T13:45:30.0000000

    • DateTimeOffset 值:

      2009-06-15T13:45:30-07:00 --> 2009-06-15T13:45:30.0000000-07:00

  • “R”、“r” : RFC1123 模式。

    2009-06-15T13:45:30 -> Mon, 15 Jun 2009 20:45:30 GMT

  • “s” : 可排序日期/时间模式。

    2009-06-15T13:45:30 (DateTimeKind.Local) -> 2009-06-15T13:45:30
    2009-06-15T13:45:30 (DateTimeKind.Utc) -> 2009-06-15T13:45:30

  • “t” : 短时间模式。

    2009-06-15T13:45:30 -> 1:45 PM (en-US)
    2009-06-15T13:45:30 -> 13:45 (hr-HR)
    2009-06-15T13:45:30 -> 01:45 م (ar-EG)

  • “T” : 长时间模式。

    2009-06-15T13:45:30 -> 1:45:30 PM (en-US)
    2009-06-15T13:45:30 -> 13:45:30 (hr-HR)
    2009-06-15T13:45:30 -> 01:45:30 م (ar-EG)

  • “u” : 通用可排序日期/时间模式。

    • 带有 DateTime 值:

      2009-06-15T13:45:30 -> 2009-06-15 13:45:30Z

    • 带有 DateTimeOffset 值:

      2009-06-15T13:45:30 -> 2009-06-15 20:45:30Z

  • “U” : 通用完整日期/时间模式。

    2009-06-15T13:45:30 -> Monday, June 15, 2009 8:45:30 PM (en-US)
    2009-06-15T13:45:30 -> den 15 juni 2009 20:45:30 (sv-SE)
    2009-06-15T13:45:30 -> Δευτέρα, 15 Ιουνίου 2009 8:45:30 μμ (el-GR)

  • “Y”、“y” : 年月模式。

    2009-06-15T13:45:30 -> June, 2009 (en-US)
    2009-06-15T13:45:30 -> juni 2009 (da-DK)
    2009-06-15T13:45:30 -> Juni 2009 (id-ID)

  • 任何其他单个字符 : 未知说明符。

    引发运行时 FormatException

csharp
// Display using current (en-us) culture's short date format
DateTime thisDate = new DateTime(2008, 3, 15);
Console.WriteLine(thisDate.ToString("d"));           // Displays 3/15/2008

自定义日期和时间格式字符串

  • "d" : 一个月中的某一天(1 到 31)。

    2009-06-01T13:45:30 -> 1
    2009-06-15T13:45:30 -> 15

  • “dd” : 一个月中的某一天(01 到 31)。

    2009-06-01T13:45:30 -> 01

  • “ddd” : 一周中某天的缩写名称。

    2009-06-15T13:45:30 -> Mon (en-US)
    2009-06-15T13:45:30 -> Пн (ru-RU)
    2009-06-15T13:45:30 -> lun. (fr-FR)

  • “dddd” : 一周中某天的完整名称。

    2009-06-15T13:45:30 -> Monday (en-US)
    2009-06-15T13:45:30 -> понедельник (ru-RU)
    2009-06-15T13:45:30 -> lundi (fr-FR)

  • “f” : 日期和时间值的十分之几秒。

    2009-06-15T13:45:30.6170000 -> 6
    2009-06-15T13:45:30.05 -> 0

  • “ff” : 日期和时间值的百分之几秒。

    2009-06-15T13:45:30.6170000 -> 61
    2009-06-15T13:45:30.0050000 -> 00

  • “fff” : 日期和时间值的千分之几秒。

    6/15/2009 13:45:30.617 -> 617
    6/15/2009 13:45:30.0005 -> 000

  • “ffff” : 日期和时间值的万分之几秒。

    2009-06-15T13:45:30.6175000 -> 6175
    2009-06-15T13:45:30.0000500 -> 0000

  • “fffff” : 日期和时间值的十万分之几秒。

    2009-06-15T13:45:30.6175400 -> 61754
    6/15/2009 13:45:30.000005 -> 00000

  • “ffffff” : 日期和时间值的百万分之几秒。

    2009-06-15T13:45:30.6175420 -> 617542
    2009-06-15T13:45:30.0000005 -> 000000

  • “fffffff” : 日期和时间值的千万分之几秒。

    2009-06-15T13:45:30.6175425 -> 6175425
    2009-06-15T13:45:30.0001150 -> 0001150

  • “F” : 如果非零,则为日期和时间值的十分之几秒。

    2009-06-15T13:45:30.6170000 -> 6
    2009-06-15T13:45:30.0500000 -> (无输出)

  • “FF” : 如果非零,则为日期和时间值的百分之几秒。

    2009-06-15T13:45:30.6170000 -> 61
    2009-06-15T13:45:30.0050000 -> (无输出)

  • “FFF” : 如果非零,则为日期和时间值的千分之几秒。

    2009-06-15T13:45:30.6170000 -> 617
    2009-06-15T13:45:30.0005000 -> (无输出)

  • “FFFF” : 如果非零,则为日期和时间值的万分之几秒。

    2009-06-15T13:45:30.5275000 -> 5275
    2009-06-15T13:45:30.0000500 -> (无输出)

  • “FFFFF” : 如果非零,则为日期和时间值的十万分之几秒。

    2009-06-15T13:45:30.6175400 -> 61754
    2009-06-15T13:45:30.0000050 -> (无输出)

  • “FFFFFF” : 如果非零,则为日期和时间值的百万分之几秒。

    2009-06-15T13:45:30.6175420 -> 617542
    2009-06-15T13:45:30.0000005 -> (无输出)

  • “FFFFFFF” : 如果非零,则为日期和时间值的千万分之几秒。

    2009-06-15T13:45:30.6175425 -> 6175425
    2009-06-15T13:45:30.0001150 -> 000115

  • “g”、“gg” : 时期或纪元。

    2009-06-15T13:45:30.6170000 -> A.D.

  • “h” : 采用 12 小时制的小时(从 1 到 12)。

    2009-06-15T01:45:30 -> 1
    2009-06-15T13:45:30 -> 1

  • “hh” : 采用 12 小时制的小时从(01 到 12)。

    2009-06-15T01:45:30 -> 01
    2009-06-15T13:45:30 -> 01

  • “H” : 采用 24 小时制的小时(从 0 到 23)。

    2009-06-15T01:45:30 -> 1
    2009-06-15T13:45:30 -> 13

  • “HH” : 采用 24 小时制的小时(从 00 到 23)。

    2009-06-15T01:45:30 -> 01

  • “K” : 时区信息。

    • DateTime 值:

      2009-06-15T13:45:30, Kind Unspecified ->
      2009-06-15T13:45:30, Kind Utc -> Z
      2009-06-15T13:45:30, Kind Local -> -07:00(取决于本地计算机的设置)

    • DateTimeOffset 值:

      2009-06-15T01:45:30-07:00 --> -07:00
      2009-06-15T08:45:30+00:00 --> +00:00

  • “m” : 分钟(0 到 59)。

    2009-06-15T01:09:30 -> 9
    2009-06-15T13:29:30 -> 29

  • “mm” : 分钟(00 到 59)。

    2009-06-15T01:09:30 -> 09
    2009-06-15T01:45:30 -> 45

  • “M” : 月份(1 到 12)。

    2009-06-15T13:45:30 -> 6

  • “MM” : 月份(1 到 12)。

    2009-06-15T13:45:30 -> 06

  • “MMM” : 月份的缩写名称。

    2009-06-15T13:45:30 -> Jun (en-US)
    2009-06-15T13:45:30 -> juin (fr-FR)
    2009-06-15T13:45:30 -> Jun (zu-ZA)

  • “MMMM” : 月份的完整名称。

    2009-06-15T13:45:30 -> June (en-US)
    2009-06-15T13:45:30 -> juni (da-DK)
    2009-06-15T13:45:30 -> uJuni (zu-ZA)

  • “s” : 秒(0 到 59)。

    2009-06-15T13:45:09 -> 9

  • “ss” : 秒(00 到 59)。

    2009-06-15T13:45:09 -> 09

  • “t” : AM/PM 指示符的第一个字符。

    2009-06-15T13:45:30 -> P (en-US)
    2009-06-15T13:45:30 -> 午 (ja-JP)
    2009-06-15T13:45:30 -> (fr-FR)

  • “tt” : AM/PM 指示符。

    2009-06-15T13:45:30 -> PM (en-US)
    2009-06-15T13:45:30 -> 午後 (ja-JP)

  • “y” : 年份(0 到 99)。

    0001-01-01T00:00:00 -> 1
    0900-01-01T00:00:00 -> 0
    1900-01-01T00:00:00 -> 0
    2009-06-15T13:45:30 -> 9
    2019-06-15T13:45:30 -> 19

  • “yy” : 年份(00 到 99)。

    0001-01-01T00:00:00 -> 01
    0900-01-01T00:00:00 -> 00
    1900-01-01T00:00:00 -> 00

  • “yyy” : 年份(最少三位数字)。

    0001-01-01T00:00:00 -> 001
    0900-01-01T00:00:00 -> 900
    1900-01-01T00:00:00 -> 1900
    2009-06-15T13:45:30 -> 2009

  • “yyyy” : 由四位数字表示的年份。

    0001-01-01T00:00:00 -> 0001
    0900-01-01T00:00:00 -> 0900

  • “yyyyy” : 由五位数字表示的年份。

    0001-01-01T00:00:00 -> 00001
    2009-06-15T13:45:30 -> 02009

  • “z” : 相对于 UTC 的小时偏移量,无前导零。

    2009-06-15T13:45:30-07:00 -> -7

  • “zz” : 相对于 UTC 的小时偏移量,带有表示一位数值的前导零。

    2009-06-15T13:45:30-07:00 -> -07

  • “zzz” : 相对于 UTC 的小时和分钟偏移量。

    2009-06-15T13:45:30-07:00 -> -07:00

  • ":" : 时间分隔符。

    2009-06-15T13:45:30 -> : (en-US)
    2009-06-15T13:45:30 -> . (it-IT)
    2009-06-15T13:45:30 -> : (ja-JP)

  • "/" : 日期分隔符。

    2009-06-15T13:45:30 -> / (en-US)
    2009-06-15T13:45:30 -> - (ar-DZ)
    2009-06-15T13:45:30 -> . (tr-TR)

  • "string "'string ' : 文本字符串分隔符。

    2009-06-15T13:45:30 ("arr:" h:m t) -> arr:1:45 P
    2009-06-15T13:45:30 ('arr:' h:m t) -> arr:1:45 P

  • % : 将下面的字符定义为自定义格式说明符。

    2009-06-15T13:45:30 (%h) -> 1

  • \ : 转义字符。

    2009-06-15T13:45:30 (h \h) -> 1 h

  • 任何其他字符 : 字符将复制到未更改的结果字符串。

    2009-06-15T01:45:30 (arr hh:mm t) -> arr 01:45 A

csharp
DateTime thisDate1 = new DateTime(2011, 6, 10);
Console.WriteLine("Today is " + thisDate1.ToString("MMMM dd, yyyy") + ".");
 
DateTimeOffset thisDate2 = new DateTimeOffset(2011, 6, 10, 15, 24, 16, 
                                              TimeSpan.Zero);
Console.WriteLine("The current date and time: {0:MM/dd/yy H:mm:ss zzz}", 
                   thisDate2); 
// The example displays the following output:
//    Today is June 10, 2011.
//    The current date and time: 06/10/11 15:24:16 +00:00

枚举类型

枚举类型(所有派生自 System.Enum 的类型)

  • Gg

    如有可能,将枚举项显示为字符串值,否则显示当前实例的整数值。 如果枚举使用 Flags 属性集进行定义,则每个有效项的字符串值会连接在一起(以逗号分隔)。如果未设置 Flags 属性,则将无效值显示为数字项。

    下面的示例演示 G 格式说明符。

    csharp
    Console.WriteLine(ConsoleColor.Red.ToString("G"));         // Displays Red
    FileAttributes attributes = FileAttributes.Hidden |
                                FileAttributes.Archive;
    Console.WriteLine(attributes.ToString("G"));   // Displays Hidden, Archive
  • Ff

    如有可能,将枚举项显示为字符串值。 如果值可以完全显示为枚举中项的总和(即使未提供 Flags 属性),则每个有效项的字符串值会连接在一起(以逗号分隔)。如果值不能由枚举项完全确定,则值会格式化为整数值。

    下面的示例演示 F 格式说明符。

    csharp
    Console.WriteLine(ConsoleColor.Blue.ToString("F"));       // Displays Blue
    FileAttributes attributes = FileAttributes.Hidden | 
                                FileAttributes.Archive;
    Console.WriteLine(attributes.ToString("F"));   // Displays Hidden, Archive
  • Dd

    以尽可能短的表示形式将枚举项显示为整数值。

    下面的示例演示 D 格式说明符。

    csharp
    Console.WriteLine(ConsoleColor.Cyan.ToString("D"));         // Displays 11
    FileAttributes attributes = FileAttributes.Hidden |
                                FileAttributes.Archive;
    Console.WriteLine(attributes.ToString("D"));                // Displays 34
  • Xx

    将枚举项显示为十六进制值。根据需要以前导零表示此值,以确保在枚举类型的基础数值类型中,结果字符串的每个字节都有两个字符。下面的示例演示 X 格式说明符。在示例中,这两者的基础类型 ConsoleColorFileAttributesInt32,或 32 位(或 4 字节)整数,它将生成 8 个字符的结果字符串。

    csharp
    Console.WriteLine(ConsoleColor.Cyan.ToString("X"));   // Displays 0000000B
    FileAttributes attributes = FileAttributes.Hidden |
                                FileAttributes.Archive;
    Console.WriteLine(attributes.ToString("X"));          // Displays 00000022

数值类型

数值类型:BigIntegerByteDecimalDoubleInt16Int32Int64SByteSingleUInt16UInt32UInt64

标准数字格式字符串

标准数字格式字符串用于格式化通用数值类型。标准数字格式字符串采用 Axx 的形式,其中:

  • A 是称为 “格式说明符” 的单个字母字符。任何包含一个以上字母字符(包括空白)的数字格式字符串都被解释为自定义数字格式字符串。有关更多信息,请参见自定义数字格式字符串。

  • xx 是称为 “精度说明符” 的可选整数。精度说明符的范围从 0 到 99,并且影响结果中的位数。请注意,精度说明符控制数字的字符串表示形式中的数字个数。它不舍入该数字。若要执行舍入运算,请使用 Math.CeilingMath.FloorMath.Round 方法。

当精度说明符控制结果字符串中的小数位数时,结果字符串会反映一个数字,该数字四舍五入到最接近无限精确结果的可表示结果。如果有两个同样接近的可表示结果:

  • 在 .NET Framework 和.NET Core(.NET Core 2.0 及以下)上,运行时选择最低有效数字更高的结果(即使用 MidpointRounding.AwayFromZero) 。

  • 在 .NET Core 2.1 及更高版本上,运行时选择最低有效数字为偶数的结果(即使用 MidpointRounding.ToEven) 。

格式说明符:

  • “C”或“c” : 货币(结果:货币值。)

    123.456 ("C", en-US) -> $123.46
    123.456 ("C", fr-FR) -> 123,46 €
    123.456 ("C", ja-JP) -> ¥123
    -123.456 ("C3", en-US) -> ($123.456)
    -123.456 ("C3", fr-FR) -> -123,456 €
    -123.456 ("C3", ja-JP) -> -¥123.456

  • “D”或“d” : 十进制(结果:整型数字,负号可选。)

    1234 ("D") -> 1234
    -1234 ("D6") -> -001234

  • “E”或“e” : 指数(科学型)(结果:指数表示法。)

    1052.0329112756 ("E", en-US) -> 1.052033E+003
    1052.0329112756 ("e", fr-FR) -> 1,052033e+003
    -1052.0329112756 ("e2", en-US) -> -1.05e+003
    -1052.0329112756 ("E2", fr-FR) -> -1,05E+003

  • “F”或“f” : 定点(结果:整数和十进制小数,负号可选。)

    1234.567 ("F", en-US) -> 1234.57
    1234.567 ("F", de-DE) -> 1234,57
    1234 ("F1", en-US) -> 1234.0
    1234 ("F1", de-DE) -> 1234,0
    -1234.56 ("F4", en-US) -> -1234.5600
    -1234.56 ("F4", de-DE) -> -1234,5600

  • “G”或“g” : 常规(结果:更紧凑的定点表示法或科学记数法。)

    -123.456 ("G", en-US) -> -123.456
    -123.456 ("G", sv-SE) -> -123,456
    123.4546 ("G4", en-US) -> 123.5
    123.4546 ("G4", sv-SE) -> 123,5
    -1.234567890e-25 ("G", en-US) -> -1.23456789E-25
    -1.234567890e-25 ("G", sv-SE) -> -1,23456789E-25

  • “N”或“n” : 数字(结果:整数和十进制小数、组分隔符和十进制小数分隔符,负号可选。)

    1234.567 ("N", en-US) -> 1,234.57
    1234.567 ("N", ru-RU) -> 1 234,57
    1234 ("N1", en-US) -> 1,234.0
    1234 ("N1", ru-RU) -> 1 234,0
    -1234.56 ("N3", en-US) -> -1,234.560
    -1234.56 ("N3", ru-RU) -> -1 234,560

  • “P”或“p” : 百分比(结果:数字乘以 100 并以百分比符号显示。)

    1 ("P", en-US) -> 100.00 %
    1 ("P", fr-FR) -> 100,00 %
    -0.39678 ("P1", en-US) -> -39.7 %
    -0.39678 ("P1", fr-FR) -> -39,7 %

  • “R”或“r” : 往返过程(结果:可以往返至相同数字的字符串。)

    123456789.12345678 ("R") -> 123456789.12345678
    -1234567890.12345678 ("R") -> -1234567890.1234567

  • “X”或“x” : 十六进制(结果:十六进制字符串。)

    255 ("X") -> FF
    -1 ("x") -> ff
    255 ("x4") -> 00ff
    -1 ("X4") -> 00FF

  • 任何其他单个字符 : 未知说明符(结果:在运行时引发 FormatException。)

csharp
decimal value = 123.456m;
Console.WriteLine(value.ToString("C2"));
// Displays $123.46

Console.WriteLine("Your account balance is {0:C2}.", value);
// Displays "Your account balance is $123.46."

decimal[] amounts = { 16305.32m, 18794.16m };
Console.WriteLine("   Beginning Balance           Ending Balance");
Console.WriteLine("   {0,-28:C2}{1,14:C2}", amounts[0], amounts[1]);
// Displays:
//        Beginning Balance           Ending Balance
//        $16,305.32                      $18,794.16

自定义数字格式字符串

  • “0” : 零占位符(用对应的数字(如果存在)替换零;否则,将在结果字符串中显示零。)

    1234.5678 ("00000") -> 01235
    0.45678 ("0.00", en-US) -> 0.46
    0.45678 ("0.00", fr-FR) -> 0,46

  • "#" : 数字占位符(用对应的数字(如果存在)替换“#”符号;否则,不会在结果字符串中显示任何数字。)

    1234.5678 ("#####") -> 1235
    0.45678 ("#.##", en-US) -> .46
    0.45678 ("#.##", fr-FR) -> ,46

  • "." : 小数点(确定小数点分隔符在结果字符串中的位置。)

    0.45678 ("0.00", *en-US*) -> 0.46

  • "," : 组分隔符和数字比例换算(用作组分隔符和数字比例换算说明符。 作为组分隔符时,它在各个组之间插入本地化的组分隔符字符。 作为数字比例换算说明符,对于每个指定的逗号,它将数字除以 1000。)

    • 组分隔符说明符:

      2147483647 ("##,#", en-US) -> 2,147,483,647
      2147483647 ("##,#", es-ES) -> 2.147.483.647

    • 比例换算说明符:

      2147483647 ("#,#,,", en-US) -> 2,147
      2147483647 ("#,#,,", es-ES) -> 2.147

  • "%" : 百分比占位符(将数字乘以 100,并在结果字符串中插入本地化的百分比符号。)

    0.3697 ("%#0.00", en-US) -> %36.97
    0.3697 ("%#0.00", el-GR) -> %36,97
    0.3697 ("##.0 %", en-US) -> 37.0 %
    0.3697 ("##.0 %", el-GR) -> 37,0 %

  • "‰" : 千分比占位符(将数字乘以 1000,并在结果字符串中插入本地化的千分比符号。)

    0.03697 ("#0.00‰", *en-US*) -> 36.97‰
    0.03697 ("#0.00‰", ru-RU) -> 36,97‰

  • “E0”“E+0”“E-0”“e0”“e+0”“e-0” : 指数表示法(如果后跟至少一个 0(零),则使用指数表示法设置结果格式。 “E”或“e”指示指数符号在结果字符串中是大写还是小写。 跟在“E”或“e”字符后面的零的数目确定指数中的最小位数。 加号 (+) 指示符号字符总是置于指数前面。 减号 (-) 指示符号字符仅置于负指数前面。)

    987654 ("#0.0e0") -> 98.8e4
    1503.92311 ("0.0##e+00") -> 1.504e+03
    1.8901385E-16 ("0.0e+00") -> 1.9e-16

  • “\” : 转义符(使下一个字符被解释为文本而不是自定义格式说明符。)

    987654 ("\###00\#") -> #987654#

  • 'string '"string" : 文本字符串分隔符(指示应复制到未更改的结果字符串的封闭字符。)

    68 ("# 'degrees'") -> 68 degrees
    68 ("# ' degrees'") -> 68 degrees

  • ; : 部分分隔符(通过分隔格式字符串定义正数、负数和零各部分。)

    12.345 ("#0.0#;(#0.0#);-\0-") -> 12.35
    0 ("#0.0#;(#0.0#);-\0-") -> -0-
    -12.345 ("#0.0#;(#0.0#);-\0-") -> (12.35)
    12.345 ("#0.0#;(#0.0#)") -> 12.35
    0 ("#0.0#;(#0.0#)") -> 0.0
    -12.345 ("#0.0#;(#0.0#)") -> (12.35)

  • 其他 : 所有其他字符(字符将复制到未更改的结果字符串。)

    68 ("# °") -> 68 °

csharp
double value;
 
value = 123;
Console.WriteLine(value.ToString("00000"));
Console.WriteLine(String.Format("{0:00000}", value));
// Displays 00123

value = 1.2;
Console.WriteLine(value.ToString("#.##", CultureInfo.InvariantCulture));
Console.WriteLine(String.Format(CultureInfo.InvariantCulture, 
                                "{0:#.##}", value));
// Displays 1.2

value = 1.2;
Console.WriteLine(value.ToString("0.00", CultureInfo.InvariantCulture));
Console.WriteLine(String.Format(CultureInfo.InvariantCulture, 
                                "{0:0.00}", value));
// Displays 1.20

value = 1234567890;
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));
Console.WriteLine(String.Format(CultureInfo.InvariantCulture, 
                                "{0:#,#}", value));
// Displays 1,234,567,890

value = .086;
Console.WriteLine(value.ToString("#0.##%", CultureInfo.InvariantCulture));
Console.WriteLine(String.Format(CultureInfo.InvariantCulture, 
                                "{0:#0.##%}", value));
// Displays 8.6%

value = .00354;
string perMilleFmt = "#0.## " + '\u2030';
Console.WriteLine(value.ToString(perMilleFmt, CultureInfo.InvariantCulture));
Console.WriteLine(String.Format(CultureInfo.InvariantCulture, 
                                "{0:" + perMilleFmt + "}", value));
// Displays 3.54‰

value = 86000;
Console.WriteLine(value.ToString("0.###E+0", CultureInfo.InvariantCulture));
Console.WriteLine(String.Format(CultureInfo.InvariantCulture, 
                                "{0:0.###E+0}", value));
// Displays 8.6E+4

int ivalue = 123;
Console.WriteLine(ivalue.ToString("\\#\\#\\# ##0 dollars and \\0\\0 cents \\#\\#\\#"));
Console.WriteLine(String.Format("{0:\\#\\#\\# ##0 dollars and \\0\\0 cents \\#\\#\\#}",
                                ivalue));
// Displays ### 123 dollars and 00 cents ###

double posValue = 1234;
double negValue = -1234;
double zeroValue = 0;

string fmt2 = "##;(##)";
string fmt3 = "##;(##);**Zero**";

Console.WriteLine(posValue.ToString(fmt2));  
Console.WriteLine(String.Format("{0:" + fmt2 + "}", posValue));    
// Displays 1234

Console.WriteLine(negValue.ToString(fmt2));  
Console.WriteLine(String.Format("{0:" + fmt2 + "}", negValue));    
// Displays (1234)

Console.WriteLine(zeroValue.ToString(fmt3)); 
Console.WriteLine(String.Format("{0:" + fmt3 + "}", zeroValue));
// Displays **Zero**

double n = 123.8;
Console.WriteLine($"{n:#,##0.0K}");
// The example displays the following output:
//      123.8K

Guid

  • N :32 位数:

    00000000000000000000000000000000

  • D :32 的数字,由连字符分隔:

    00000000-0000-0000-0000-000000000000

  • B :32 位,用连字符隔开,括在大括号中:

    {00000000-0000-0000-0000-000000000000}

  • P :32 位,用连字符隔开,括在括号中:

    (00000000-0000-0000-0000-000000000000)

  • X :括在大括号中的四个十六进制值,其中第四个值是八个十六进制值的子集(也括在大括号中):

    {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

csharp
static void Main(string[] args)
{
    Guid guid = Guid.NewGuid();

    Console.WriteLine($"guid.ToString(): {guid.ToString()}");
    Console.WriteLine($"guid.ToString(null): {guid.ToString(null)}");
    Console.WriteLine($"guid.ToString(\"\"): {guid.ToString("")}");
    Console.WriteLine($"guid.ToString(\"N\"): {guid.ToString("N")}");
    Console.WriteLine($"guid.ToString(\"D\"): {guid.ToString("D")}");
    Console.WriteLine($"guid.ToString(\"B\"): {guid.ToString("B")}");
    Console.WriteLine($"guid.ToString(\"P\"): {guid.ToString("P")}");
    Console.WriteLine($"guid.ToString(\"X\"): {guid.ToString("X")}");

    Console.ReadLine();
}
// Output:
// guid.ToString(): 60125f52-256b-4e27-9d0a-7c64611e4d3e
// guid.ToString(null): 60125f52-256b-4e27-9d0a-7c64611e4d3e
// guid.ToString(""): 60125f52-256b-4e27-9d0a-7c64611e4d3e
// guid.ToString("N"): 60125f52256b4e279d0a7c64611e4d3e
// guid.ToString("D"): 60125f52-256b-4e27-9d0a-7c64611e4d3e
// guid.ToString("B"): {60125f52-256b-4e27-9d0a-7c64611e4d3e}
// guid.ToString("P"): (60125f52-256b-4e27-9d0a-7c64611e4d3e)
// guid.ToString("X"): {0x60125f52,0x256b,0x4e27,{0x9d,0x0a,0x7c,0x64,0x61,0x1e,0x4d,0x3e}}

TimeSpan

标准 TimeSpan 格式字符串

  • “c” : 常量(固定)格式(此说明符不区分区域性。 它的形式是 [-][d'.']hh':'mm':'ss['.'fffffff]。)

    TimeSpan.Zero -> 00:00:00
    New TimeSpan(0, 0, 30, 0) -> 00:30:00
    New TimeSpan(3, 17, 25, 30, 500) -> 3.17:25:30.5000000

  • “g” : 常规短格式(该说明符仅输出需要的内容。 它区分区域性并采用 [-][d':']h':'mm':'ss[.FFFFFFF] 形式。)

    New TimeSpan(1, 3, 16, 50, 500) -> 1:3:16:50.5 (en-US)
    New TimeSpan(1, 3, 16, 50, 500) -> 1:3:16:50,5 (fr-FR)
    New TimeSpan(1, 3, 16, 50, 599) -> 1:3:16:50.599 (en-US)
    New TimeSpan(1, 3, 16, 50, 599) -> 1:3:16:50,599 (fr-FR)

  • “G” : 常规长格式(此说明符始终输出天数和七个小数位。 它区分区域性并采用 [-]d':'hh':'mm':'ss.fffffff 形式。)

    New TimeSpan(18, 30, 0) -> 0:18:30:00.0000000 (en-US)
    New TimeSpan(18, 30, 0) -> 0:18:30:00,0000000 (fr-FR)

csharp
using System;

public class Example
{
   public static void Main()
   {
      TimeSpan duration = new TimeSpan(1, 12, 23, 62);
      string output = "Time of Travel: " + duration.ToString("c");
      Console.WriteLine(output);
      
      Console.WriteLine("Time of Travel: {0:c}", duration); 
   }
}
// The example displays the following output:
//       Time of Travel: 1.12:24:02
//       Time of Travel: 1.12:24:02

自定义 TimeSpan 格式字符串

  • “d”,“%d” : 时间间隔中的整天数。

    new TimeSpan(6, 14, 32, 17, 685):
    %d --> "6"
    d\.hh\:mm --> "6.14:32"

  • "dd"-"dddddddd" : 时间间隔中的整天数,根据需要使用前导零填充。

    new TimeSpan(6, 14, 32, 17, 685):
    ddd --> "006"
    dd\.hh\:mm --> "06.14:32"

  • “h”,“%h” : 时间间隔中不计为天数一部分的整小时数。 一位数小时数没有前导零。

    new TimeSpan(6, 14, 32, 17, 685):
    %h --> "14"
    hh\:mm --> "14:32"

  • “hh” : 时间间隔中不计为天数一部分的整小时数。 一位数小时具有前导零。

    new TimeSpan(6, 14, 32, 17, 685):
    hh --> "14"
    new TimeSpan(6, 8, 32, 17, 685):
    hh --> 08

  • “m”,“%m” : 时间间隔中不包含在小时或天数中的整分钟数。 一位数分钟数没有前导零。

    new TimeSpan(6, 14, 8, 17, 685):
    %m --> "8"
    h\:m --> "14:8"

  • “mm” : 时间间隔中不包含在小时或天数中的整分钟数。 一位数分钟具有前导零。

    new TimeSpan(6, 14, 8, 17, 685):
    mm --> "08"
    new TimeSpan(6, 8, 5, 17, 685):
    d\.hh\:mm\:ss --> 6.08:05:17

  • “s”,“%s” : 时间间隔中不包含在小时、天数或分钟中的整秒数。 一位数秒数没有前导零。

    TimeSpan.FromSeconds(12.965):
    %s --> 12
    s\.fff --> 12.965

  • “ss” : 时间间隔中不包含在小时、天数或分钟中的整秒数。 一位数秒具有前导零。

    TimeSpan.FromSeconds(6.965):
    ss --> 06
    ss\.fff --> 06.965

  • “f”,“%f” : 时间间隔中的十分之几秒。

    TimeSpan.FromSeconds(6.895):
    f --> 8
    ss\.f --> 06.8

  • “ff” : 时间间隔中的百分之几秒。

    TimeSpan.FromSeconds(6.895):
    ff --> 89
    ss\.ff --> 06.89

  • “fff” : 时间间隔中的毫秒。

    TimeSpan.FromSeconds(6.895):
    fff --> 895
    ss\.fff --> 06.895

  • “ffff” : 时间间隔中的万分之几秒。

    TimeSpan.Parse("0:0:6.8954321"):
    ffff --> 8954
    ss\.ffff --> 06.8954

  • “fffff” : 时间间隔中的十万分之几秒。

    TimeSpan.Parse("0:0:6.8954321"):
    fffff --> 89543
    ss\.fffff --> 06.89543

  • “ffffff” : 时间间隔中的百万分之几秒。

    TimeSpan.Parse("0:0:6.8954321"):
    ffffff --> 895432
    ss\.ffffff --> 06.895432

  • “fffffff” : 时间间隔中的千万分之几秒(或小数时钟周期)。

    TimeSpan.Parse("0:0:6.8954321"):
    fffffff --> 8954321
    ss\.fffffff --> 06.8954321

  • “F”,“%F” : 时间间隔中的十分之几秒。 如果该数字为零,则不显示任何内容。

    TimeSpan.Parse("00:00:06.32"):
    %F --> 3
    TimeSpan.Parse("0:0:3.091"):
    ss\.F --> 03.

  • “FF” : 时间间隔中的百分之几秒。 不包含任何小数尾随零或两个零位。

    TimeSpan.Parse("00:00:06.329"):
    FF --> 32
    TimeSpan.Parse("0:0:3.101"):
    ss\.FF --> 03.1

  • “FFF” : 时间间隔中的毫秒。 不包含任何小数尾随零。

    TimeSpan.Parse("00:00:06.3291"):
    FFF --> 329
    TimeSpan.Parse("0:0:3.1009"):
    ss\.FFF --> 03.1

  • “FFFF” : 时间间隔中的万分之几秒。 不包含任何小数尾随零。

    TimeSpan.Parse("00:00:06.32917"):
    FFFFF --> 3291
    TimeSpan.Parse("0:0:3.10009"):
    ss\.FFFF --> 03.1

  • “FFFFF” : 时间间隔中的十万分之几秒。 不包含任何小数尾随零。

    TimeSpan.Parse("00:00:06.329179"):
    FFFFF --> 32917
    TimeSpan.Parse("0:0:3.100009"):
    ss\.FFFFF --> 03.1

  • “FFFFFF” : 时间间隔中的百万分之几秒。 不显示任何小数尾随零。

    TimeSpan.Parse("00:00:06.3291791"):
    FFFFFF --> 329179
    TimeSpan.Parse("0:0:3.1000009"):
    ss\.FFFFFF --> 03.1

  • “FFFFFFF” : 时间间隔中的千万分之几秒。 不显示任何小数尾随零或七个零。

    TimeSpan.Parse("00:00:06.3291791"):
    FFFFFF --> 3291791
    TimeSpan.Parse("0:0:3.1900000"):
    ss\.FFFFFF --> 03.19

  • 'string ' : 文本字符串分隔符。

    new TimeSpan(14, 32, 17):
    hh':'mm':'ss --> "14:32:17"

  • \ : 转义字符。

    new TimeSpan(14, 32, 17):
    hh\:mm\:ss --> "14:32:17"

  • 任何其他字符 : 任何其他未转义字符会解释为自定义格式说明符。

    new TimeSpan(14, 32, 17):
    hh\:mm\:ss --> "14:32:17"

csharp
using System;

public class Example
{
   public static void Main()
   {
      TimeSpan duration = new TimeSpan(1, 12, 23, 62);

      string output = null;
      output = "Time of Travel: " + duration.ToString("%d") + " days";
      Console.WriteLine(output);
      output = "Time of Travel: " + duration.ToString(@"dd\.hh\:mm\:ss"); 
      Console.WriteLine(output);

      Console.WriteLine("Time of Travel: {0:%d} day(s)", duration);
      Console.WriteLine("Time of Travel: {0:dd\\.hh\\:mm\\:ss} days", duration);
   }
}
// The example displays the following output:
//       Time of Travel: 1 days
//       Time of Travel: 01.12:24:02
//       Time of Travel: 1 day(s)
//       Time of Travel: 01.12:24:02 days

文章内容主要整理自 MSDN

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.