我有以下代码。我想要做的是如果它是一个空值的日期返回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