转换为日期时间时,字符串未被识别为有效的DateTime

本文关键字:识别 有效 DateTime 字符串 日期 时间 转换 | 更新日期: 2023-09-27 17:58:45

我在尝试插入一行数据时遇到以下错误。它表示该字符串未被识别为有效的日期时间。

C#

protected void saveExceptionAdd(object sender, System.EventArgs e)
  {
      //Default value secruity
      if (ddlTimeFromAdd.SelectedIndex == 0 || ddlTimeToAdd.SelectedIndex == 0)
      {
          lblAddExcept.Visible = true;
          lblAddExcept.Text = "Fields Required.";
          divExceptionAdd.Focus();
      }
      else
      {
          string EFTVFROM = txtDatefromAdd.Text.ToString() + ddlTimeFromAdd.SelectedValue.ToString();
          string EFTVTO = txtDatetoAdd.ToString() + ddlTimeToAdd.SelectedValue.ToString();
          DateTime eftvfromdt = Convert.ToDateTime(EFTVFROM);
          DateTime eftvtodt = Convert.ToDateTime(EFTVTO);
          //Update WeekDay restriction
          CDSSQLConnections.RunStoredProcedureWithNParams("connDataStore", "sp_AB_BULLETIN_EXCEPTION_INSERT",
          new Dictionary<string, object> { { "EFTVFROM", eftvfromdt }, { "EFTVTO", eftvtodt }, { "ABSTATUS", ddlStatus.SelectedValue }, { "LASTMODBY", CDSSecurity.CurrentUserID } });
          divExceptionAdd.Visible = false;
          lblAddExcept.Visible = false;
          repException.DataBind();
      }
  }

转换为日期时间时,字符串未被识别为有效的DateTime

您可以告诉解析器应该使用哪种日期时间格式来解析字符串

DateTime dt=DateTime.ParseExact("24/01/2013", "dd/MM/yyyy", CultureInfo.InvariantCulture);