我想以12小时格式输出日期时间。它适用于某些然后中断。在C#中格式化日期时间#
代码:
while (myReader.Read())
{
Console.WriteLine("Date Before formatting = " + myReader["Date"].ToString());
DateTime dt = DateTime.ParseExact(myReader["Date"].ToString(), "yyyy/MM/dd hh:mm:ss", CultureInfo.InvariantCulture);
string format = "MM/dd/yyyy hh:mm:ss tt";
Console.WriteLine(myReader["RxNumber"].ToString() + "\t\t" + dt.ToString(format));
}
输出:
连接打开
日期格式= 2013年12月26日11时26分08秒
12/26/2013 11:26:08 AM
日期格式化之前之前= 2013/12/26 09:02:01
12345 2013年12月26日上午09时02分01秒
日期之前格式= 2013年12月26日9时04分29秒
123456 2013年12月26日上午09时04分29秒
日期格式= 2013年10月28日十点19分26秒
10/28/2013 10:19:26 AM
日期之前格式= 2014年2月14日12时25分57秒
7000006 2014年2月14日12:25之前:57 AM
日期之前格式= 2014年2月14日13点20分18秒
System.FormatException:字符串未被识别为有效的DateTime。 在System.DateTimeParse.ParseExact(字符串s字符串格式,的DateTimeFormatInfo dtfi,DateTimeStyles样式) 在System.DateTime.ParseExact(字符串s字符串格式,提供者的IFormatProvider) 在test.Program.Main(字串[] args)在c:\ Users \ Admin \ Desktop \用于测试test608 \ test \ test \ Program.cs的软件中:第33行 ☺按任意键继续。 。 。
你可以给我们一个引发异常的字符串值的例子吗? – Bill
我已更新我的问题以获得更好的输出。 – user3562751
您的输出与您的代码不符。我倾向于猜测迈克尔的答案是对的。 –