将字符串转换为可空的DateTime [英] Convert String to Nullable DateTime

查看:127
本文介绍了将字符串转换为可空的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何将DateTime.TryParse与Nullable< DateTime>一起使用?

我有这行代码

DateTime? dt = Condition == true ? (DateTime?)Convert.ToDateTime(stringDate) : null;

这是将字符串转换为Nullable DateTime的正确方法吗? 转换为DateTime,然后再次转换为Nullable DateTime?

Is this the correct way to convert string to Nullable DateTime, or is there a direct method to convert without converting it to DateTime and again casting it to Nullable DateTime?

推荐答案

您可以尝试以下操作:-

You can try this:-

 DateTime? dt = string.IsNullOrEmpty(date) ? (DateTime?)null : DateTime.Parse(date);

这篇关于将字符串转换为可空的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆