DateTime类型ToString时要显示AM/PM怎么办
- DateTime time = DateTime.Now;
- string s = time.ToString("yyyy.MM.dd hh:mm:ss t\\M");
- Console.WriteLine(s);
DateTime time = DateTime.Now;
string s = time.ToString("yyyy.MM.dd hh:mm:ss t\\M");
Console.WriteLine(s);
Explanation:
t gives the first character of the AM/PM designator (the localized designator can be retrieved in
DateTimeFormatInfo.AMDesignator and
DateTimeFormatInfo.PMDesignator). The
\\M escapes the
M so that
DateTime.ToString does not interpret it as part of the format string and print the numerical value of the month.
Note that once you do this you are explicitly being culture insensitive. For example, in Japan, the AM/PM designator differs in the second characters, not the first.
本文介绍了如何在C#中使用DateTime类型显示包含AM/PM标识的时间格式,并解释了格式字符串的具体用法及文化敏感性问题。
852

被折叠的 条评论
为什么被折叠?



