我想以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
您的輸出與您的代碼不符。我傾向於猜測邁克爾的答案是對的。 –