如何解析格式为“2012 年 11 月 7 日晚上 9:02”的日期字符串
我想我很接近了。试过:
string line = "Nov. 7, 2012, 9:02 p.m.";
string format = "MMM. dd, yyyy, hh:mm p.m."; ;
DateTime result = DateTime.ParseExact(line, format, null);
如何解析格式为“2012 年 11 月 7 日晚上 9:02”的日期字符串
我想我很接近了。试过:
string line = "Nov. 7, 2012, 9:02 p.m.";
string format = "MMM. dd, yyyy, hh:mm p.m."; ;
DateTime result = DateTime.ParseExact(line, format, null);
这将起作用。
string format = string.Format("{0:MMM. dd, yyyy, hh:mm tt}", DateTime.Now);
Console.WriteLine(format);
这将以您预期的格式打印结果(2012 年 11 月 7 日,晚上 9:02)
string format = "MMMM. dd, yyyy, hh:mm p.m.";
你用 4 M 的时间度过了整整一个月
尝试
string format = "MMM. dd, yyyy, hh:mm p.m.";
得到短暂的月份,即 11 月 -> 11 月
这是参考指南:http: //msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
使用单个h
而不是hh
. 此外,t.'m'.
可能会匹配您的 am/pm,具体取决于它是否区分大小写。