2017-08-04 49 views
-1
string actionDate = ((DateTime) objActionDescr.GetDataTable().Rows[0]["ActionDate"]).ToString("MM"); 

错误如我无效投射出来。请帮助如何将数据表格日期转换为特定字符串格式

+1

具体是什么错误信息? – mason

+1

什么错误?示例输入?你期望什么? –

+0

表中的ActionDate类型是什么? –

回答

0

首先,检查值是否为空,然后将其转换为您需要的任何格式。 类似这样的:

//first, check if that column contains value 
if (!objActionDescr.GetDataTable().Rows[0].IsNull("ActionDate")) 
{ 
    //if value is there, do with it what you need... 

    //this will return month name, -> like August 
    string actionDateMonth = (Convert.ToDateTime(objActionDescr.GetDataTable().Rows[0]["ActionDate"])).ToString("MM"); 
    //this will return month number -> like 08 
    string actionDateMonthNumber = (Convert.ToDateTime(objActionDescr.GetDataTable().Rows[0]["ActionDate"])).ToString("mm"); 
    //same as above 
    actionDateMonthNumber = (Convert.ToDateTime(objActionDescr.GetDataTable().Rows[0]["ActionDate"])).Month.ToString(); 
} 
+0

Thanx Nino这是疯狂的真棒,我不知道堆栈溢出如何工作,但我知道有一点给出。我怎么给你那个 – Patrick

+0

@Patrick,只要把它标记为已解决。看看[这里](https://meta.stackoverflow.com/questions/251078/how-to-update-and-accept-answers):) – Nino

相关问题