如何将null字符串转换为datetime [英] How to convert null string to datetime

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

问题描述



我想将空字符串转换为datetime.I我从数据库中获取字符串st。当我找到st = something然后它就可以了。但是当它返回null或为空时我就会出错。

Hi,
I want to convert a null string into datetime.I am getting the string st from database. When i find st= something then it works ok. But when it returns null or empty then i m getting errors.

string st=nul;
Lavel1.Text= Convert.ToDateTime(st).ToShortDateString();



代码用C#编写;



提前谢谢。


Codes are written in C#;

Thanks in advance.

推荐答案

你不能只用来判断吗? null 值?

如果为null,请将文本框留空,否则放入值。
Can't you just use an if to check the null value?
If it is null, make your textbox blank, else put the value.


这可能会有所帮助,使用DateTime类的tryparse函数



This might help, use the tryparse function of DateTime class

string st = null;
DateTime dateTime;

if (string.IsNullOrEmpty(st))
    Lavel1.Text = string.Empty;
else if( DateTime.TryParse( st , out dateTime))
{
    Lavel1.Text = dateTime.ToShortDateString();
}


查看讨论想要传递Datetime的空值 [ ^ ]


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

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