strTime = dsexcel.Tables[0].Rows[i]["TIME"].ToString();  //strTime = "1899-12-30 10:45:00"
(dsexcel.Tables[0].Rows[i]["TIME"].ToString().Split(' '))[1].Contains(":")/*当strTime = "10:45:00"时,这条语句越界了,故我需要判断strTime 是否有空格*/
不知如何写,请赐教!

解决方案 »

  1.   

    IndexOf(" ")>=0不知道你想干什么
    为什么不把它Convert.ToDateTime后直接操作日期???
      

  2.   

    if (strTime.Contains(" ") || strTime.Contains(" ") )
    {}
      

  3.   

    这个日期是从excel表格中取出来的,所以不能直接操作,要一段段的处理:
    string strTime;
                    strTime = dsexcel.Tables[0].Rows[i]["TIME"].ToString();         //strTime = "1899-12-30 10:45:00"                if ((dsexcel.Tables[0].Rows[i]["TIME"].ToString().Split(' '))[1].Contains(":")) // strTime "03:00:00" string
                    {
                        strTime = (dsexcel.Tables[0].Rows[i]["TIME"].ToString().Split(' '))[1];     //strTime = "10:30:00"
                    }
                    else
                    {
                        string tempstrtime = dsexcel.Tables[0].Rows[i]["TIME"].ToString();      //tempstrtime = "10:30:00 上午"
                        strTime = (tempstrtime.Substring(0, tempstrtime.IndexOf(' ') + 1));     //strTime = "10:30:00 "
                    }                string strDate = (dsexcel.Tables[0].Rows[i]["DATE"].ToString().Split(' '))[0];      //strDate = "2008-6-28"
                    strTempJobTitle = dsexcel.Tables[0].Rows[i]["PROGRAM"].ToString();              //strTempJobTitle = "LATE SHOW"
                    if (strTempJobTitle == "")
                    {
                        continue;
                    }                strTempChannel = tempchannel.ToString();        //strTempChannel = "2"                DateTime myDateTime = DateTime.Parse(strDate + " " + strTime, new CultureInfo("en-US", true), DateTimeStyles.NoCurrentDateDefault); //myDateTime = {2008-6-28 10:30:00}
                    strTempDate = String.Format("{0:yyyy}-{0:MM}-{0:dd}", myDateTime) + " " + String.Format("{0:HH}:{0:mm}:{0:ss}", myDateTime);    //strTempDate = "2008-06-28 10:30:00"
      

  4.   

    string temp="1899-12-30 10:45:00" if(temp.indexof(" ")==-1)
      
    // 不包含 空格
    else
    //包含 空格
      

  5.   

    strTime = DateTime.Parse(dsexcel.Tables[0].Rows[i]["TIME"]).ToString("hh:mm:ss")
      

  6.   

    strTime = (DateTime.Parse(dsexcel.Tables[0].Rows[i]["TIME"])).ToString("hh:mm:ss")
      

  7.   

    strTime = DateTime.Parse(dsexcel.Tables[0].Rows[i]["TIME"]).ToString("HH:mm:ss");
      

  8.   

    我用:
    strTime = (DateTime.Parse(dsexcel.Tables[0].Rows[i]["TIME"])).ToString("hh:mm:ss");//之后出现以下报错!
    Error 3 The best overloaded method match for 'System.DateTime.Parse(string)' has some invalid arguments F:\work_file\ExcelToXmlConverter\ExcelToXmlConverter\JobExporter.cs 1082 28 MC2JobExporter
    Error 4 Argument '1': cannot convert from 'object' to 'string' F:\work_file\ExcelToXmlConverter\ExcelToXmlConverter\JobExporter.cs 1082 43 MC2JobExporter
      

  9.   

    写清楚点:
    Error 3 The best overloaded method match for 'System.DateTime.Parse(string)' has some invalid arguments
    Error 4 Argument '1': cannot convert from 'object' to 'string'
      

  10.   

    哈哈,是这样写就对了,strTime = DateTime.Parse(dsexcel.Tables[0].Rows[i]["TIME"].ToString()).ToString("hh:mm:ss");
    多谢楼上的兄弟们!