DateTime类型ToString时要显示AM/PM怎么办

来源:互联网 发布:打码软件 编辑:程序博客网 时间:2024/06/08 00:06
DateTime类型ToString时要显示AM/PM怎么办       
 
 
  1. DateTime time = DateTime.Now; 
  2. string s = time.ToString("yyyy.MM.dd hh:mm:ss t\\M"); 
  3. Console.WriteLine(s); 


Explanation: t gives the first character of the AM/PM designator (the localized designator can be retrieved inDateTimeFormatInfo.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.