c# – 如果为null,则DateTime.Parse采用最小日期值
发布时间:2020-12-15 20:54:37 所属栏目:百科 来源:网络整理
导读:我有一个asp.net应用程序,我有一个文本框输入datetime值,它保存在数据库中. 现在,当我尝试检索日期时,如果日期为空,则显示1/1/0001 12:00:00 AM. this.FirstReceivedDate = DateTime.Parse(dr["FirstReceivedDate"].ToString()); 道歉,要求改变了.如果FirstR
我有一个asp.net应用程序,我有一个文本框输入datetime值,它保存在数据库中.
现在,当我尝试检索日期时,如果日期为空,则显示1/1/0001 12:00:00 AM. this.FirstReceivedDate = DateTime.Parse(dr["FirstReceivedDate"].ToString()); 道歉,要求改变了.如果FirstReceivedDate为null,我想显示空白(”). 我怎样才能做到这一点? 解决方法
您可以尝试以下内容:
DateTime date = DateTime.Parse(dr["FirstReceivedDate"].ToString()); this.FirstReceivedDate = date != DateTime.MinValue ? date : DateTime.Now; 因为它正确解析,这应该工作. 或者,如果您想要一个日期为null的值,您可以尝试这样的事情: 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# – .net core 2.0 ConfigureLogging xunit test
- 【学习react】react的setState函数的小bug的问题
- 我无法在Postgresql上的PgAgent上运行作业
- 正则表达式---读书笔记
- swift – 实现AVCaptureFileOutputDelegate和AVCaptureVide
- cocos2d-x android黑屏后返回游戏卡顿
- cocos2d-x中android中设置横屏和竖屏
- c# – 更改Xamarin Forms XAML按钮的isVisible属性
- cocos 语法错误:“namespace”
- sqlite遇到database is locked