将日期时间转换为字符串 [英] converting date time to string

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

问题描述

我有一个字符串变量 st ,我已经使用这个语句分配这个字符串变量来输出表单数据表

I have a string variable st and i have assign this string variable to output coming form data table by using this statement

string st;

if(dt!=null)
{
    if(dt.rows.count> 0)
    {
        st = dt.Rows[3]["timeslot_StartTime"].ToString();
    }
}

现在我想将此字符串变量转换为日期时间财产,我已经通过使用以下语句做了这一个。

now i want to convert this string variable to date time property and i have done this one by using the below statement

DateTime pt1 = DateTime.Parse(st);

但它显示错误,在st说,使用未分配的本地可变st

but it shows error at st saying that use of unassigned local varaible "st".

推荐答案

可能您使用的变量与分配的变量不同,如

Probably you are using the variable in a different scope from the one where it's assigned, like

string st;
if (condition) {
    st = dt.Rows[3]["timeslot_StartTime"].ToString();
}
DateTime pt1 = DateTime.Parse(st);

所以, st 并不总是初始化只有当if条件被验证)。尝试改用

So, st is not always initialized (it is only if the if condition is verified). Try instead

string st;
if (condition) {
    st = dt.Rows[3]["timeslot_StartTime"].ToString();
    DateTime pt1 = DateTime.Parse(st);
}

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

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