如何解决字符串未被识别为有效的日期时间



我在c#中有一个组件,我编写以下代码:

string SearchString = " and StartDate Between '" 
    + string.Format("{0:yyyy/MM/dd}", Convert.ToDateTime(calender_az.Text)) 
    + "' And '" + string.Format("{0:yyyy/MM/dd}", Convert.ToDateTime(calender_ta.Text)) 
    + "'";

它可以工作,但它有这样的错误:

字符串未被识别为有效的日期时间? calender_az是日期选择器

我该怎么办?

试试这个

DateTime AZ = DateTime.ParseExact(calender_az.ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None);

最新更新