String.Format custom date and time format string

Standard date and time format strings

Date and time format strings define the textual representation of DateTime values ​​produced by formatting operations. It also defines the representation of date and time values ​​required in parse operations to successfully convert strings to dates and times. A custom format string consists of one or more custom date and time format specifiers. Any non-standard date and time format strings are interpreted as custom date and time format strings.

format specifier illustrate The C# example is taken from Microsoft's official documentation
"d" Short date pattern. 2009-06-15T13:45:30 -> 6/15/2009 
“D” Long date pattern. 2009-06-15T13:45:30 -> Monday, June 15, 2009 
“f” Full date/time pattern (short time) 2009-06-15T13:45:30 -> Monday, June 15, 2009 1:45 PM
“F” Full date/time pattern (long time) 2009-06-15T13:45:30 -> Monday, June 15, 2009 1:45:30 PM 
“g” Regular date/time pattern (short time) 2009-06-15T13:45:30 -> 6/15/2009 1:45 PM
“G” Regular date/time pattern (long time) 2009-06-15T13:45:30 -> 6/15/2009 1:45:30 PM 
“s” Sortable date/time pattern 2009-06-15T13:45:30-> 2009-06-15T13:45:30
“t” short time mode 2009-06-15T13:45:30 -> 1:45 PM 
“T” long time mode 2009-06-15T13:45:30 -> 1:45:30 PM 
“u” Generic Sortable Date/Time Pattern 2009-06-15T13:45:30 -> 2009-06-15 13:45:30Z
“U” Generic full date/time pattern 2009-06-15T13:45:30 -> Monday, June 15, 2009 8:45:30 PM 
 “y” year month pattern 2009-06-15T13:45:30 -> June 2009 

How standard format strings work

In formatting operations, standard format strings are just aliases for custom format strings. The advantage of using an alias to refer to a custom format string is that although the alias remains fixed, the custom format string itself can vary. This is important because string representations of date and time values ​​often vary by culture. For example, the "d" standard format string indicates that date and time values ​​should be displayed using the short date pattern. For a fixed culture, this pattern is "MM/dd/yyyy".

Custom date and time format strings

format specifier illustrate The C# example is taken from Microsoft's official documentation
"d" day of the month (1 to 31) 2009-06-01T13:45:30 -> 1
2009-06-15T13:45:30 -> 15
“dd” Day of the month (01 to 31) 2009-06-01T13:45:30 -> 01
2009-06-15T13:45:30 -> 15
“ddd” Abbreviated name of the day of the week 2009-06-15T13:45:30 -> My
"dddd" The full name of the day of the week 2009-06-15T13:45:30 -> Monday
“f” Tenths of a second in a date and time value 2009-06-15T13:45:30.6170000 -> 6
2009-06-15T13:45:30.05 -> 0
“ff” The hundredths of a second in a date and time value 2009-06-15T13:45:30.6170000 -> 61
2009-06-15T13:45:30.0050000 -> 00
“fff” Milliseconds of date and time values 6/15/2009 13:45:30.617 -> 617
6/15/2009 13:45:30.0005 -> 000
“ffff” Ten-thousandths of a second in date and time values 2009-06-15T13:45:30.6175000 -> 6175
2009-06-15T13:45:30.0000500 -> 0000
“fffff” Hundred-thousandths of a second in a date and time value 2009-06-15T13:45:30.6175400 -> 61754
6/15/2009 13:45:30.000005 -> 00000
"ffffff" Millionths of a second in a date and time value 2009-06-15T13:45:30.6175420 -> 617542
2009-06-15T13:45:30.0000005 -> 000000
“fffffff” Millionths of a second in a date and time value 2009-06-15T13:45:30.6175425 -> 6175425
2009-06-15T13:45:30.0001150 -> 0001150
“F” If nonzero, the tenths of a second of the date and time value 2009-06-15T13:45:30.6170000 -> 6
2009-06-15T13:45:30.0500000 -> (no output)
“FF” If nonzero, the hundredths of a second in the date and time value. 2009-06-15T13:45:30.6170000 -> 61
2009-06-15T13:45:30.0050000 -> (no output)
“FFF” If nonzero, the thousandths of a second for date and time values 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
2009-06-15T13:45:30 -> 13
“K” 时区信息 2009-06-15T13:45:30, Kind Unspecified ->
2009-06-15T13:45:30, Kind Utc -> Z
2009-06-15T13:45:30, Kind Local -> -07: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
“MMMM” 月份的完整名称 2009-06-15T13:45:30 -> June 
“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 
“tt” AM/PM 指示符 2009-06-15T13:45:30 -> PM 
“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
2019-06-15T13:45:30 -> 19
“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
1900-01-01T00:00:00 -> 1900
2009-06-15T13:45:30 -> 2009
“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 -> : 
"/" 日期分隔符。 2009-06-15T13:45:30 -> / 
"string"

'string'
文本字符串分隔符 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

 示例:在Unity中测试

using System;
using UnityEngine;

public class StringText : MonoBehaviour
{

    void Start()
    {
        //21:37:09
        Debug.LogFormat("{0:HH:mm:ss}", DateTime.Now);
        //09:37:09 下午
        Debug.LogFormat("{0:hh:mm:ss tt}", DateTime.Now);
        //2022-06-10
        Debug.LogFormat("{0:yyyy-MM-dd}", DateTime.Now);
        //2022:06:10
        Debug.LogFormat("{0:yyyy:MM:dd}", DateTime.Now);
        //2022/06/10
        Debug.LogFormat("{0:yyyy/MM/dd}", DateTime.Now);
        //2022年06月10日
        Debug.LogFormat("{0:yyyy年MM月dd日}", DateTime.Now);
        //2022年06月10日 09:39:55 下午
        Debug.LogFormat("{0:yyyy年MM月dd日 hh:mm:ss tt}", DateTime.Now);
        //2022年06月10日 21:40:32
        Debug.LogFormat("{0:yyyy年MM月dd日 HH:mm:ss}", DateTime.Now);
        //A.D. 2022年06月10日 21:42:14
        Debug.LogFormat("{0:g yyyy年MM月dd日 HH:mm:ss}", DateTime.Now);
        //2022 - 06 - 10 21:43:21Z
        Debug.LogFormat("{0:u}", DateTime.Now);
        //2022年6月10日 13:47:04
        Debug.LogFormat("{0:U}", DateTime.Now);
        //21:47
        Debug.LogFormat("{0:t}", DateTime.Now);
        //21:47:04
        Debug.LogFormat("{0:T}", DateTime.Now);
        //2022-06-10T21:47:04
        Debug.LogFormat("{0:s}", DateTime.Now);
        //2022/6/10 21:47
        Debug.LogFormat("{0:g}", DateTime.Now);
        //2022/6/10 21:47:04
        Debug.LogFormat("{0:G}", DateTime.Now);
        //2022年6月10日 21:47:04
        Debug.LogFormat("{0:F}", DateTime.Now);
        //2022年6月10日 21:47
        Debug.LogFormat("{0:f}", DateTime.Now);
        //2022年6月10日
        Debug.LogFormat("{0:D}", DateTime.Now);
        //2022/6/10
        Debug.LogFormat("{0:d}", DateTime.Now);
        //六月
        Debug.LogFormat("{0:MMMM}", DateTime.Now);
        //21
        Debug.LogFormat("{0:HH}", DateTime.Now);
    }

}

运行效果:

 上述测试打印出来的Log

Log
21:58:00
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:10)

Log
09:58:00 下午
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:12)

Log
2022-06-10
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:14)

Log
2022:06:10
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:16)

Log
2022/06/10
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:18)

Log
2022年06月10日
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:20)

Log
2022年06月10日 09:58:00 下午
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:22)

Log
2022年06月10日 21:58:00
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:24)

Log
A.D. 2022年06月10日 21:58:00
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:26)

Log
2022-06-10 21:58:00Z
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:28)

Log
2022年6月10日 13:58:00
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:30)

Log
21:58
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:32)

Log
21:58:00
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:34)

Log
2022-06-10T21:58:00
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:36)

Log
2022/6/10 21:58
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:38)

Log
2022/6/10 21:58:00
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:40)

Log
2022年6月10日 21:58:00
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:42)

Log
2022年6月10日 21:58
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:44)

Log
2022年6月10日
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:46)

Log
2022/6/10
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:48)

Log
六月
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:50)

Log
21
UnityEngine.Debug:LogFormat (string,object[])
StringText:Start () (at Assets/StringText.cs:52)

一个应用示例演示:

 

Guess you like

Origin blog.csdn.net/hack_yin/article/details/125227661