我有一个 asp.net 应用程序,其中有一个用于输入日期时间值的文本框,它保存在数据库中。
现在,当我尝试检索日期时,如果日期为空,它会显示 1/1/0001 12:00:00 AM。
this.FirstReceivedDate = DateTime.Parse(dr["FirstReceivedDate"].ToString());
抱歉,要求已更改。现在,如果 FirstReceivedDate 为空,我想显示空白 ('')。
我该怎么做?
最佳答案
你可以试试下面的方法:
DateTime date = DateTime.Parse(dr["FirstReceivedDate"].ToString());
this.FirstReceivedDate = date != DateTime.MinValue ? date : DateTime.Now;
因为它解析正确,这应该可以工作。
或者,如果您想要日期为空时的值,您可以尝试这样的操作:
DateTime date;
if(DateTime.TryParse(dr["FirstReceivedDate"], out date))
this.FirstReceivedDate = date != DateTime.MinValue ? date : DateTime.Now;
else
this.FirstReceivedDate = DateTime.Now; // or whatever you want to do if "FirstReceivedDate" is not a valid date.
关于c# - DateTime.Parse 如果为 null,则采用最小日期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32096781/