【问题标题】:String was not recognized as a valid DateTime [Remove Time from DateTime]字符串未被识别为有效的日期时间 [从日期时间中删除时间]
【发布时间】:2016-01-28 04:24:33
【问题描述】:

我正在从我的 sql server 中检索一个日期,即 2016-01-06(year-month-day) 到一个字符串中。 sql 格式为日期。我将获得 1/6/2016/(month/day/year) 12:00:00 AM ,而不是仅获得 2016-01-06 。现在我要做的是删除时间并将日期转换为 dd/M/yyyy 格式。我已经按照其他问题中的所有示例进行了操作,但我会得到“String 未被识别为有效的 DateTime” 是否有任何步骤我错过了或我做错了。

 protected void btnUser_Click(object sender, EventArgs e)
        {
            {
                string Name = cmbName.Text;
                string start = "";

                SqlConnection myConn = new SqlConnection("Data Source=localhost;" + "Initial Catalog=IBBTS_DB; Integrated Security =SSPI");

                    SqlCommand retrieveStart_DateCmd = new SqlCommand("SELECT startDate FROM testSet where TS_ID = 121 ;", myConn);
                    SqlDataReader reader6 = retrieveStart_DateCmd.ExecuteReader();
                    while (reader6.Read())
                    {
                        start = (reader6.GetValue(0).ToString());
                    }
                    reader6.Close();  

                    DateTime dateTime = DateTime.ParseExact(start, "dd/MM/yyyy", CultureInfo.InvariantCulture);


             }
        }

【问题讨论】:

  • 为什么不尝试使用转换功能。

标签: c# asp.net sql-server date datetime


【解决方案1】:

SQL 以 SQL 的标准格式存储 datetime 数据类型日期。但是,当您从 SQL 检索 datetime 数据类型时,datetime 结果将转换为 C# 中的 DateTime 结构。

由于检索到的值是 DateTime 对象,因此您无需转换为字符串然后将其解析出来。这样做会使检索 datetime 的转换工作加倍。

试试这个例子。

protected void btnUser_Click(object sender, EventArgs e)
    {
        {
            string Name = cmbName.Text;
            DateTime start = default(DateTime);

            SqlConnection myConn = new SqlConnection("Data Source=localhost;" + "Initial Catalog=IBBTS_DB; Integrated Security =SSPI");

                SqlCommand retrieveStart_DateCmd = new SqlCommand("SELECT startDate FROM testSet where TS_ID = 121 ;", myConn);
                SqlDataReader reader6 = retrieveStart_DateCmd.ExecuteReader();
                while (reader6.Read())
                {
                    start = (DateTime)reader6[0];
                }
                reader6.Close();  
                string myFormattedString = start.ToString("dd/MM/yyyy");
         }
    }

【讨论】:

    【解决方案2】:

    既然您知道该字段将是一个日期,而且它只是一个字段,您可以使用 GetDateTime 吗?

    DateTime dateTime = Reader6.GetDateTime(0)
    

    这可能会让您跳过字符串转换。显然,如果有可能,首先检查 dbNull。

    【讨论】:

      【解决方案3】:

      避免使用ToString() - 您正在简化 SQL Server 发送给您的类型。您应该能够使用GetDateTime(0) 而不是GateValue(0) 来检索该值。这将使您可以更轻松地操纵该值。

      C# 没有内置的Date 类型,只有DateTime。因此,这就是您所看到的。得到DateTime 后,您可以使用普通的 Format 操作来随意格式化它。

      【讨论】:

        【解决方案4】:

        尝试以这种格式分配日期:

        protected void btnUser_Click(object sender, EventArgs e)
        { 
                string Name = cmbName.Text, start = string.empty;
        
                SqlConnection myConn = new SqlConnection("Data Source=localhost;" + "Initial Catalog=IBBTS_DB; Integrated Security =SSPI");
        
                SqlCommand retrieveStart_DateCmd = new SqlCommand("SELECT startDate FROM testSet where TS_ID = 121 ;", myConn);
                SqlDataReader reader6 = retrieveStart_DateCmd.ExecuteReader();
        
                while (reader6.Read())
                {
                      start = (reader6.GetValue(0).ToString());
                }
                reader6.Close();  
        
                DateTime dateTime = DateTime.ParseExact(start, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);//DateTime.ParseExact(start, "dd/MM/yyyy", CultureInfo.InvariantCulture); 
        }
        

        并在你的 Web.config 文件下添加这个

        <configuration>
            <system.web>
                 <globalization culture="en-US" uiCulture="en-US" />
            </system.web>
        

        【讨论】:

          猜你喜欢
          • 2013-02-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-15
          • 2012-04-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多