DateTime time = DateTime.Now;
string s = time.ToString("yyyy.MM.dd hh:mm:ss t\\M");
Console.WriteLine(s);
Explanation:tgives the first character of the AM/PM designator (the localized designator can be retrieved inDateTimeFormatInfo.AMDesignatorandDateTimeFormatInfo.PMDesignator). The\\Mescapes theMso thatDateTime.ToStringdoes 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 标记,并通过转义字符避免月份被错误解析。
854

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



