我有以下代碼。我想要做的是如果它是一個空值的日期返回NULL或返回短日期。例如:12/12/15。我得到一個錯誤:將字符串轉換爲shortdate時間
When converting a string to DateTime parse the string to take the date before putting each variable into the DateTime Object.
public DateTime? StrToDate(string val)
{
DateTime? dt = string.IsNullOrEmpty(val)
? (DateTime?)null
: DateTime.ParseExact(val, "MM/dd/yyyy", null);
return dt;
}
你傳遞給函數的字符串是什麼? – JoaoFSA
首先,你究竟在哪裏得到那個錯誤?這並非我見過的錯誤。其次,你在一年中傳遞2位數字,但解析方法顯式尋找4。 – DavidG